@@ -25,58 +25,98 @@ extension LabelView: ConfigurableItem {
2525
2626 public struct Model : AlignmentProvider {
2727
28- // MARK: - Nested types
28+ // MARK: - Editor
2929
30- public struct TextStyle : Equatable {
30+ public struct Property : Editor {
31+ public typealias Model = LabelView . Model
3132
32- public let color : UIColor
33- public let font : UIFont
33+ private let closure : ( Model ) -> Model
3434
35- public init ( color: UIColor = . black, font: UIFont = . systemFont( ofSize: 16 ) ) {
36- self . color = color
37- self . font = font
35+ public init ( closure: @escaping ( Model ) -> Model ) {
36+ self . closure = closure
3837 }
3938
40- }
39+ public func edit( _ model: Model ) -> Model {
40+ return closure ( model)
41+ }
4142
42- public struct TextLayout : Equatable {
43+ public static func text( _ value: TextValue ) -> Property {
44+ . init( closure: { model in
45+ var model = model
46+ model. set ( text: value)
47+ return model
48+ } )
49+ }
4350
44- public let lineBreakMode : NSLineBreakMode
45- public let numberOfLines : Int
51+ public static func style( _ value: TextStyle ) -> Property {
52+ . init( closure: { model in
53+ var model = model
54+ model. set ( style: value)
55+ return model
56+ } )
57+ }
4658
47- public init ( lineBreakMode: NSLineBreakMode = . byWordWrapping, numberOfLines: Int = 0 ) {
48- self . lineBreakMode = lineBreakMode
49- self . numberOfLines = numberOfLines
59+ public static func layout( _ value: TextLayout ) -> Property {
60+ . init( closure: { model in
61+ var model = model
62+ model. set ( layout: value)
63+ return model
64+ } )
5065 }
5166
52- }
67+ public static func alignment( _ value: Alignment ) -> Property {
68+ . init( closure: { model in
69+ var model = model
70+ model. set ( alignment: value)
71+ return model
72+ } )
73+ }
5374
54- public enum TextType : Equatable {
55- case string( String )
56- /// Mind that attributed string may re-configure other model's properties.
57- case attributedString( NSAttributedString )
75+ public static func textAlignment( _ value: NSTextAlignment ) -> Property {
76+ . init( closure: { model in
77+ var model = model
78+ model. set ( textAlignment: value)
79+ return model
80+ } )
81+ }
5882 }
5983
6084 // MARK: - Public properties
6185
62- public let text : TextType
63- public let style : TextStyle
64- public let layout : TextLayout
65- public let alignment : Alignment
66- public let textAlignment : NSTextAlignment
86+ private ( set ) public var text : TextValue = . string ( " " )
87+ private ( set ) public var style : TextStyle = . init ( )
88+ private ( set ) public var layout : TextLayout = . init ( )
89+ private ( set ) public var alignment : Alignment = . all ( . zero )
90+ private ( set ) public var textAlignment : NSTextAlignment = . left
6791
68- // MARK: - Initialization
92+ // MARK: - Mutation
6993
70- public init ( text: TextType ,
71- style: TextStyle ,
72- layout: TextLayout ,
73- textAlignment: NSTextAlignment ,
74- viewAlignment: Alignment = . all( . zero) ) {
94+ mutating func set( text: TextValue ) {
7595 self . text = text
96+ }
97+
98+ mutating func set( style: TextStyle ) {
7699 self . style = style
100+ }
101+
102+ mutating func set( layout: TextLayout ) {
77103 self . layout = layout
104+ }
105+
106+ mutating func set( alignment: Alignment ) {
107+ self . alignment = alignment
108+ }
109+
110+ mutating func set( textAlignment: NSTextAlignment ) {
78111 self . textAlignment = textAlignment
79- self . alignment = viewAlignment
112+ }
113+
114+ // MARK: - Builder
115+
116+ public static func build( @EditorBuilder < Property > content: ( Property . Type ) -> [ Property ] ) -> Self {
117+ return content ( Property . self) . reduce ( . init( ) , { model, editor in
118+ editor. edit ( model)
119+ } )
80120 }
81121
82122 }
@@ -110,7 +150,7 @@ private extension LabelView {
110150 wrap ( subview: label, with: . zero)
111151 }
112152
113- func configureText( with text: Model . TextType ) {
153+ func configureText( with text: TextValue ) {
114154 switch text {
115155 case . string( let text) :
116156 label. text = text
0 commit comments