Advertisement
Guest User

Untitled

a guest
May 25th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. import Foundation
  2. protocol KeyPathBuildable {}
  3.  
  4. extension KeyPathBuildable {
  5. func updated<Value>(keyPath: WritableKeyPath<Self, Value>, value: Value) -> Self {
  6. var object = self
  7. object[keyPath: keyPath] = value
  8. return object
  9. }
  10. }
  11. struct LabelStyle: KeyPathBuildable {
  12. private(set) var font: UIFont
  13. private(set) var textColor: UIColor
  14. private(set) var textAligment: NSTextAlignment
  15. private(set) var numberOfLines: Int
  16. private(set) var adjustsFontSizeToFitWidth: Bool
  17. private(set) var minimumScaleFactor: CGFloat?
  18. private(set) var lineBreakMode: NSLineBreakMode
  19.  
  20. func font(_ fontStyle: FontStyle, ofSize size: CGFloat? = nil) -> LabelStyle {
  21. return updated(keyPath: \.font, value: font.nunitoFont(fontStyle, ofSize: size))
  22. }
  23. func ofSize(_ size: CGFloat) -> LabelStyle {
  24. return updated(keyPath: \.font, value: font.withSize(size))
  25. }
  26. func textColor(_ textColor: UIColor) -> LabelStyle {
  27. return updated(keyPath: \.textColor, value: textColor)
  28. }
  29. func textAligemnt(_ textAligment: NSTextAlignment) -> LabelStyle {
  30. return updated(keyPath: \.textAligment, value: textAligment)
  31. }
  32.  
  33. func adjustsFontSizeToFitWidth(_ adjustsFontSizeToFitWidth: Bool) -> LabelStyle {
  34. return updated(keyPath: \.adjustsFontSizeToFitWidth, value: adjustsFontSizeToFitWidth)
  35. }
  36.  
  37. func numberOfLines(_ numberOfLines: Int) -> LabelStyle {
  38. return updated(keyPath: \.numberOfLines, value: numberOfLines)
  39. }
  40.  
  41. func lineBreakMode(_ lineBreakMode: NSLineBreakMode) -> LabelStyle {
  42. return updated(keyPath: \.lineBreakMode, value: lineBreakMode)
  43. }
  44. static let `default` = LabelStyle(font: .nunitoRegular(ofSize: UIFont.systemFontSize),
  45. textColor: .white,
  46. textAligment: .left,
  47. numberOfLines: 0,
  48. adjustsFontSizeToFitWidth: true,
  49. minimumScaleFactor: nil,
  50. lineBreakMode: .byWordWrapping)
  51. }
  52. extension UILabel {
  53. func apply(style: LabelStyle) {
  54. font = style.font
  55. textColor = style.textColor
  56. textAlignment = style.textAligment
  57. adjustsFontSizeToFitWidth = style.adjustsFontSizeToFitWidth
  58. style.minimumScaleFactor.map { minimumScaleFactor = $0 }
  59. numberOfLines = style.numberOfLines
  60. lineBreakMode = style.lineBreakMode
  61. }
  62. static func makeForAutolayout(with style: LabelStyle) -> Self {
  63. let label = self.makeForAutolayout()
  64. label.apply(style: style)
  65. return label
  66. }
  67. }
  68.  
  69. extension UILabel {
  70. static var bigWhite: UILabel {
  71. return UILabel.makeForAutolayout(with: LabelStyle
  72. .default
  73. .font(.bold, ofSize: 26)
  74. )
  75. }
  76. static var smallWhite: UILabel {
  77. return UILabel
  78. .makeForAutolayout(with: LabelStyle
  79. .default
  80. .font(.bold, ofSize: 13)
  81. )
  82. }
  83. static var smallOrange: UILabel {
  84. return UILabel
  85. .makeForAutolayout(with: LabelStyle
  86. .default
  87. .font(.bold, ofSize: 16)
  88. .textColor(Color.textOrange)
  89. )
  90. }
  91. static var smallBrown: UILabel {
  92. return UILabel
  93. .makeForAutolayout(with: LabelStyle
  94. .default
  95. .font(.bold, ofSize: 16)
  96. .textColor(Color.textBrown)
  97. )
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement