@@ -13,7 +13,7 @@ public struct InputFieldVM: ComponentVM {
13
13
14
14
/// The corner radius of the input field.
15
15
///
16
- /// /// Defaults to `.medium`.
16
+ /// Defaults to `.medium`.
17
17
public var cornerRadius : ComponentRadius = . medium
18
18
19
19
/// The font used for the input field's text.
@@ -65,7 +65,7 @@ public struct InputFieldVM: ComponentVM {
65
65
public var tintColor : UniversalColor = . accent
66
66
67
67
/// The title displayed on the input field.
68
- public var title : String = " "
68
+ public var title : String ?
69
69
70
70
/// Initializes a new instance of `InputFieldVM` with default values.
71
71
public init ( ) { }
@@ -88,6 +88,13 @@ extension InputFieldVM {
88
88
return UniversalFont . Component. large
89
89
}
90
90
}
91
+ var height : CGFloat {
92
+ return switch self . size {
93
+ case . small: 40
94
+ case . medium: 60
95
+ case . large: 80
96
+ }
97
+ }
91
98
var horizontalPadding : CGFloat {
92
99
switch self . cornerRadius {
93
100
case . none, . small, . medium, . large, . custom:
@@ -96,6 +103,9 @@ extension InputFieldVM {
96
103
return 16
97
104
}
98
105
}
106
+ var spacing : CGFloat {
107
+ return self . title. isNotNilAndEmpty ? 12 : 0
108
+ }
99
109
var backgroundColor : UniversalColor {
100
110
if let color {
101
111
return color. main. withOpacity ( 0.25 )
@@ -118,56 +128,6 @@ extension InputFieldVM {
118
128
var placeholderColor : UniversalColor {
119
129
return self . foregroundColor. withOpacity ( self . isEnabled ? 0.7 : 0.3 )
120
130
}
121
- func titleColor( for position: InputFieldTitlePosition ) -> UniversalColor {
122
- switch position {
123
- case . top:
124
- return self . foregroundColor
125
- case . center:
126
- return self . foregroundColor. withOpacity ( self . isEnabled ? 0.8 : 0.45 )
127
- }
128
- }
129
- func titleFont( for position: InputFieldTitlePosition ) -> UniversalFont {
130
- switch position {
131
- case . top:
132
- return self . preferredFont. withRelativeSize ( - 1 )
133
- case . center:
134
- let relativePadding : CGFloat = switch self . size {
135
- case . small: 1.5
136
- case . medium: 2
137
- case . large: 3
138
- }
139
- return self . preferredFont. withRelativeSize ( relativePadding)
140
- }
141
- }
142
- }
143
-
144
- // MARK: - Layout Helpers
145
-
146
- extension InputFieldVM {
147
- var inputFieldTopPadding : CGFloat {
148
- switch self . size {
149
- case . small: 30
150
- case . medium: 34
151
- case . large: 38
152
- }
153
- }
154
- var inputFieldHeight : CGFloat {
155
- switch self . size {
156
- case . small: 26
157
- case . medium: 28
158
- case . large: 30
159
- }
160
- }
161
- var verticalPadding : CGFloat {
162
- switch self . size {
163
- case . small: 12
164
- case . medium: 14
165
- case . large: 16
166
- }
167
- }
168
- var height : CGFloat {
169
- return self . inputFieldHeight + self . inputFieldTopPadding + self . verticalPadding
170
- }
171
131
}
172
132
173
133
// MARK: - UIKit Helpers
@@ -182,13 +142,17 @@ extension InputFieldVM {
182
142
. foregroundColor: self . placeholderColor. uiColor
183
143
] )
184
144
}
185
- func nsAttributedTitle( for position: InputFieldTitlePosition ) -> NSAttributedString {
145
+ var nsAttributedTitle : NSAttributedString ? {
146
+ guard let title else {
147
+ return nil
148
+ }
149
+
186
150
let attributedString = NSMutableAttributedString ( )
187
151
attributedString. append ( NSAttributedString (
188
- string: self . title,
152
+ string: title,
189
153
attributes: [
190
- . font: self . titleFont ( for : position ) . uiFont,
191
- . foregroundColor: self . titleColor ( for : position ) . uiColor
154
+ . font: self . preferredFont . uiFont,
155
+ . foregroundColor: self . foregroundColor . uiColor
192
156
]
193
157
) )
194
158
if self . isRequired {
@@ -201,7 +165,7 @@ extension InputFieldVM {
201
165
attributedString. append ( NSAttributedString (
202
166
string: " * " ,
203
167
attributes: [
204
- . font: self . titleFont ( for : position ) . uiFont,
168
+ . font: self . preferredFont . uiFont,
205
169
. foregroundColor: UniversalColor . danger. uiColor
206
170
]
207
171
) )
@@ -211,36 +175,22 @@ extension InputFieldVM {
211
175
func shouldUpdateLayout( _ oldModel: Self ) -> Bool {
212
176
return self . size != oldModel. size
213
177
|| self . horizontalPadding != oldModel. horizontalPadding
178
+ || self . spacing != oldModel. spacing
179
+ || self . cornerRadius != oldModel. cornerRadius
214
180
}
215
181
}
216
182
217
- // MARK: - UIKit Helpers
183
+ // MARK: - SwiftUI Helpers
218
184
219
185
extension InputFieldVM {
220
186
var autocorrectionType : UITextAutocorrectionType {
221
187
return self . isAutocorrectionEnabled ? . yes : . no
222
188
}
223
- func attributedTitle(
224
- for position: InputFieldTitlePosition
225
- ) -> AttributedString {
226
- var attributedString = AttributedString ( )
227
-
228
- var attributedTitle = AttributedString ( self . title)
229
- attributedTitle. font = self . titleFont ( for: position) . font
230
- attributedTitle. foregroundColor = self . titleColor ( for: position) . uiColor
231
- attributedString. append ( attributedTitle)
232
-
233
- if self . isRequired {
234
- var space = AttributedString ( " " )
235
- space. font = . systemFont( ofSize: 5 )
236
- attributedString. append ( space)
237
-
238
- var requiredSign = AttributedString ( " * " )
239
- requiredSign. font = self . titleFont ( for: position) . font
240
- requiredSign. foregroundColor = UniversalColor . danger. uiColor
241
- attributedString. append ( requiredSign)
189
+ var attributedTitle : AttributedString ? {
190
+ guard let nsAttributedTitle else {
191
+ return nil
242
192
}
243
193
244
- return attributedString
194
+ return AttributedString ( nsAttributedTitle )
245
195
}
246
196
}
0 commit comments