Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Foundation
- protocol KeyPathBuildable {}
- extension KeyPathBuildable {
- func updated<Value>(keyPath: WritableKeyPath<Self, Value>, value: Value) -> Self {
- var object = self
- object[keyPath: keyPath] = value
- return object
- }
- }
- struct LabelStyle: KeyPathBuildable {
- private(set) var font: UIFont
- private(set) var textColor: UIColor
- private(set) var textAligment: NSTextAlignment
- private(set) var numberOfLines: Int
- private(set) var adjustsFontSizeToFitWidth: Bool
- private(set) var minimumScaleFactor: CGFloat?
- private(set) var lineBreakMode: NSLineBreakMode
- func font(_ fontStyle: FontStyle, ofSize size: CGFloat? = nil) -> LabelStyle {
- return updated(keyPath: \.font, value: font.nunitoFont(fontStyle, ofSize: size))
- }
- func ofSize(_ size: CGFloat) -> LabelStyle {
- return updated(keyPath: \.font, value: font.withSize(size))
- }
- func textColor(_ textColor: UIColor) -> LabelStyle {
- return updated(keyPath: \.textColor, value: textColor)
- }
- func textAligemnt(_ textAligment: NSTextAlignment) -> LabelStyle {
- return updated(keyPath: \.textAligment, value: textAligment)
- }
- func adjustsFontSizeToFitWidth(_ adjustsFontSizeToFitWidth: Bool) -> LabelStyle {
- return updated(keyPath: \.adjustsFontSizeToFitWidth, value: adjustsFontSizeToFitWidth)
- }
- func numberOfLines(_ numberOfLines: Int) -> LabelStyle {
- return updated(keyPath: \.numberOfLines, value: numberOfLines)
- }
- func lineBreakMode(_ lineBreakMode: NSLineBreakMode) -> LabelStyle {
- return updated(keyPath: \.lineBreakMode, value: lineBreakMode)
- }
- static let `default` = LabelStyle(font: .nunitoRegular(ofSize: UIFont.systemFontSize),
- textColor: .white,
- textAligment: .left,
- numberOfLines: 0,
- adjustsFontSizeToFitWidth: true,
- minimumScaleFactor: nil,
- lineBreakMode: .byWordWrapping)
- }
- extension UILabel {
- func apply(style: LabelStyle) {
- font = style.font
- textColor = style.textColor
- textAlignment = style.textAligment
- adjustsFontSizeToFitWidth = style.adjustsFontSizeToFitWidth
- style.minimumScaleFactor.map { minimumScaleFactor = $0 }
- numberOfLines = style.numberOfLines
- lineBreakMode = style.lineBreakMode
- }
- static func makeForAutolayout(with style: LabelStyle) -> Self {
- let label = self.makeForAutolayout()
- label.apply(style: style)
- return label
- }
- }
- extension UILabel {
- static var bigWhite: UILabel {
- return UILabel.makeForAutolayout(with: LabelStyle
- .default
- .font(.bold, ofSize: 26)
- )
- }
- static var smallWhite: UILabel {
- return UILabel
- .makeForAutolayout(with: LabelStyle
- .default
- .font(.bold, ofSize: 13)
- )
- }
- static var smallOrange: UILabel {
- return UILabel
- .makeForAutolayout(with: LabelStyle
- .default
- .font(.bold, ofSize: 16)
- .textColor(Color.textOrange)
- )
- }
- static var smallBrown: UILabel {
- return UILabel
- .makeForAutolayout(with: LabelStyle
- .default
- .font(.bold, ofSize: 16)
- .textColor(Color.textBrown)
- )
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement