Skip to content

Commit 8efeeca

Browse files
add caption and titleFont for input fields
1 parent 0a9ca09 commit 8efeeca

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
lines changed

Examples/DemosApp/DemosApp/ComponentsPreview/PreviewPages/InputFieldPreview.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ struct InputFieldPreview: View {
3434
Form {
3535
AutocapitalizationPicker(selection: self.$model.autocapitalization)
3636
Toggle("Autocorrection Enabled", isOn: self.$model.isAutocorrectionEnabled)
37+
Toggle("Caption", isOn: .init(
38+
get: {
39+
return self.model.caption != nil
40+
},
41+
set: { newValue in
42+
self.model.caption = newValue ? Self.caption : nil
43+
}
44+
))
45+
CaptionFontPicker(title: "Caption Font", selection: self.$model.captionFont)
3746
ComponentOptionalColorPicker(selection: self.$model.color)
3847
ComponentRadiusPicker(selection: self.$model.cornerRadius) {
3948
Text("Custom: 20px").tag(ComponentRadius.custom(20))
@@ -69,6 +78,7 @@ struct InputFieldPreview: View {
6978
self.model.title = newValue ? Self.title : nil
7079
}
7180
))
81+
BodyFontPicker(title: "Title Font", selection: self.$model.titleFont)
7282
Picker("Title Position", selection: self.$model.titlePosition) {
7383
Text("Inside").tag(InputFieldVM.TitlePosition.inside)
7484
Text("Outside").tag(InputFieldVM.TitlePosition.outside)
@@ -87,12 +97,14 @@ struct InputFieldPreview: View {
8797
}
8898
}
8999

90-
private static let title = "Title"
91-
private static let placeholder = "Placeholder"
100+
private static let title = "Email"
101+
private static let placeholder = "Enter your email"
102+
private static let caption = "We'll send you a verification code"
92103
private static var initialModel: InputFieldVM {
93104
return .init {
94105
$0.title = Self.title
95106
$0.placeholder = Self.placeholder
107+
$0.caption = Self.caption
96108
}
97109
}
98110
}

Sources/ComponentsKit/Components/InputField/Models/InputFieldVM.swift

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ public struct InputFieldVM: ComponentVM {
88
/// Defaults to `.sentences`, which capitalizes the first letter of each sentence.
99
public var autocapitalization: TextAutocapitalization = .sentences
1010

11+
public var caption: String?
12+
13+
public var captionFont: UniversalFont?
14+
1115
/// The color of the input field.
1216
public var color: ComponentColor?
1317

@@ -69,6 +73,8 @@ public struct InputFieldVM: ComponentVM {
6973
/// The title displayed on the input field.
7074
public var title: String?
7175

76+
public var titleFont: UniversalFont?
77+
7278
public var titlePosition: TitlePosition = .inside
7379

7480
/// Initializes a new instance of `InputFieldVM` with default values.
@@ -92,6 +98,34 @@ extension InputFieldVM {
9298
return .lgBody
9399
}
94100
}
101+
var preferredTitleFont: UniversalFont {
102+
if let titleFont {
103+
return titleFont
104+
}
105+
106+
switch self.size {
107+
case .small:
108+
return .smBody
109+
case .medium:
110+
return .mdBody
111+
case .large:
112+
return .lgBody
113+
}
114+
}
115+
var preferredCaptionFont: UniversalFont {
116+
if let captionFont {
117+
return captionFont
118+
}
119+
120+
switch self.size {
121+
case .small:
122+
return .smCaption
123+
case .medium:
124+
return .mdCaption
125+
case .large:
126+
return .lgCaption
127+
}
128+
}
95129
var height: CGFloat {
96130
return switch self.size {
97131
case .small: 40
@@ -124,8 +158,7 @@ extension InputFieldVM {
124158
}
125159
}
126160
var foregroundColor: UniversalColor {
127-
let color = self.color?.main ?? .foreground
128-
return color.enabled(self.isEnabled)
161+
return (self.color?.main ?? .foreground).enabled(self.isEnabled)
129162
}
130163
var placeholderColor: UniversalColor {
131164
if let color {
@@ -134,6 +167,9 @@ extension InputFieldVM {
134167
return .secondaryForeground.enabled(self.isEnabled)
135168
}
136169
}
170+
var captionColor: UniversalColor {
171+
return (self.color?.main ?? .secondaryForeground).enabled(self.isEnabled)
172+
}
137173
var borderWidth: CGFloat {
138174
switch self.style {
139175
case .light:
@@ -178,7 +214,7 @@ extension InputFieldVM {
178214
attributedString.append(NSAttributedString(
179215
string: title,
180216
attributes: [
181-
.font: self.preferredFont.uiFont,
217+
.font: self.preferredTitleFont.uiFont,
182218
.foregroundColor: self.foregroundColor.uiColor
183219
]
184220
))
@@ -192,7 +228,7 @@ extension InputFieldVM {
192228
attributedString.append(NSAttributedString(
193229
string: "*",
194230
attributes: [
195-
.font: self.preferredFont.uiFont,
231+
.font: self.preferredTitleFont.uiFont,
196232
.foregroundColor: UniversalColor.danger.uiColor
197233
]
198234
))

Sources/ComponentsKit/Components/InputField/SUInputField.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ public struct SUInputField<FocusValue: Hashable>: View {
104104
lineWidth: self.model.borderWidth
105105
)
106106
)
107+
108+
if let caption = self.model.caption, caption.isNotEmpty {
109+
Text(caption)
110+
.font(self.model.preferredCaptionFont.font)
111+
.foregroundStyle(self.model.captionColor.color)
112+
}
107113
}
108114
}
109115
}

Sources/ComponentsKit/Components/InputField/UKInputField.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ open class UKInputField: UIView, UKComponent {
3434
public var titleLabel = UILabel()
3535
/// An underlying text field from the standard library.
3636
public var textField = UITextField()
37+
/// A label that displays the caption from the model.
38+
public var captionLabel = UILabel()
3739
/// A view that contains `horizontalStackView` and `titleLabel` whet it is outside.
3840
public var textFieldContainer = UIView()
3941
/// A stack view that contains `textField` and `titleLabel` whet it is inside.
@@ -94,6 +96,7 @@ open class UKInputField: UIView, UKComponent {
9496
self.verticalStackView.addArrangedSubview(self.titleLabel)
9597
}
9698
self.verticalStackView.addArrangedSubview(self.textFieldContainer)
99+
self.verticalStackView.addArrangedSubview(self.captionLabel)
97100
self.horizontalStackView.addArrangedSubview(self.textField)
98101
self.textFieldContainer.addSubview(self.horizontalStackView)
99102

@@ -123,6 +126,7 @@ open class UKInputField: UIView, UKComponent {
123126
Self.Style.verticalStackView(self.verticalStackView, model: self.model)
124127
Self.Style.textField(self.textField, model: self.model)
125128
Self.Style.titleLabel(self.titleLabel, model: self.model)
129+
Self.Style.captionLabel(self.captionLabel, model: self.model)
126130
}
127131

128132
// MARK: Layout
@@ -245,6 +249,15 @@ extension UKInputField {
245249
textField.autocorrectionType = model.autocorrectionType
246250
textField.autocapitalizationType = model.autocapitalization.textAutocapitalizationType
247251
}
252+
static func captionLabel(
253+
_ label: UILabel,
254+
model: Model
255+
) {
256+
label.text = model.caption
257+
label.isVisible = model.caption.isNotNilAndEmpty
258+
label.textColor = model.captionColor.uiColor
259+
label.font = model.preferredCaptionFont.uiFont
260+
}
248261
static func horizontalStackView(
249262
_ stackView: UIStackView,
250263
model: Model

0 commit comments

Comments
 (0)