✨ Syntactic sugar for Swift
Initialize UILabel then set its properties.
private let titleLabel = UILabel().then {
$0.textAlignment = .center
$0.adjustsFontSizeToFitWidth = true
$0.transform = CGAffineTransform(rotationAngle: -.pi / 6)
}
This is the same as:
private let titleLabel: UILabel = {
let titleLabel = UILabel()
titleLabel.textAlignment = .center
titleLabel.adjustsFontSizeToFitWidth = true
titleLabel.transform = CGAffineTransform(rotationAngle: -.pi / 6)
return titleLabel
}()
-
You can use
then()
to all ofNSObject
subclasses.let animation = CABasicAnimation(keyPath: "fillColor").then { $0.fromValue = fromColor.cgColor $0.toValue = toColor.cgColor $0.duration = duration $0.beginTime = CACurrentMediaTime() + delay $0.timingFunction = CAMediaTimingFunction(name: .easeOut) }
-
You can make extensions for your own types.
extension TransportType: Then {} let instance = TransportType.car.then { $0.availability = .full }
-
Use
with()
when copying the value types.let frame = oldFrame.with { $0.size.width = 200 $0.size.height = 100 } frame.width // will be 200 frame.height // will be 100
-
Use
do()
to do something with less typing.UserDefaults.standard.do { $0.set(username, forKey: .username) $0.set(email, forKey: .email) $0.synchronize() }
Here's an example usage in a real project.
// MARK: - NotificationsCell
final class NotificationsCell: UITableViewCell {
// MARK: - Properties
/// Container
private let containerView = UIView()
/// Notification image view
private let notificationImageView = UIImageView().then {
$0.image = Asset.Avatar.avatarPlaceholder.image
$0.contentMode = .scaleAspectFill
}
/// Notification title label
private let titleLabel = UILabel().then {
$0.setContentHuggingPriority(.defaultHigh, for: .horizontal)
$0.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
$0.numberOfLines = LayoutConstants.titleNumberOfLines
}
/// Notification subtitle label
private let subtitleLabel = UILabel().then {
$0.numberOfLines = LayoutConstants.subtitleNumberOfLines
}
/// Date of notification completion label
private let dateLabel = UILabel().then {
$0.setContentHuggingPriority(.defaultLow, for: .horizontal)
$0.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
$0.textAlignment = .right
}
...
}
-
Using CocoaPods:
pod 'incetro-then'
-
Using Swift Package Manager:
import PackageDescription let package = Package( name: "MyAwesomeApp", dependencies: [ .package(url: "https://github.com/Incetro/Then", .branch("main")), ] )
Then is under MIT license. See the LICENSE file for more info.