Skip to content

Commit cc2788b

Browse files
change appearance of UKInputField
1 parent f435fc6 commit cc2788b

File tree

4 files changed

+63
-231
lines changed

4 files changed

+63
-231
lines changed

Sources/SwiftComponents/InputField/Models/InputFieldTitlePosition.swift

Lines changed: 0 additions & 4 deletions
This file was deleted.

Sources/SwiftComponents/InputField/Models/InputFieldVM.swift

Lines changed: 20 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ extension InputFieldVM {
8888
return UniversalFont.Component.large
8989
}
9090
}
91+
var height: CGFloat {
92+
return switch self.size {
93+
case .small: 40
94+
case .medium: 60
95+
case .large: 80
96+
}
97+
}
9198
var horizontalPadding: CGFloat {
9299
switch self.cornerRadius {
93100
case .none, .small, .medium, .large, .custom:
@@ -97,7 +104,7 @@ extension InputFieldVM {
97104
}
98105
}
99106
var spacing: CGFloat {
100-
return 12
107+
return self.title.isNotNilAndEmpty ? 12 : 0
101108
}
102109
var backgroundColor: UniversalColor {
103110
if let color {
@@ -121,60 +128,6 @@ extension InputFieldVM {
121128
var placeholderColor: UniversalColor {
122129
return self.foregroundColor.withOpacity(self.isEnabled ? 0.7 : 0.3)
123130
}
124-
func titleColor(for position: InputFieldTitlePosition) -> UniversalColor {
125-
switch position {
126-
case .top:
127-
return self.foregroundColor
128-
case .center:
129-
return self.foregroundColor.withOpacity(self.isEnabled ? 0.8 : 0.45)
130-
}
131-
}
132-
func titleFont(for position: InputFieldTitlePosition) -> UniversalFont {
133-
switch position {
134-
case .top:
135-
return self.preferredFont.withRelativeSize(-1)
136-
case .center:
137-
let relativePadding: CGFloat = switch self.size {
138-
case .small: 1.5
139-
case .medium: 2
140-
case .large: 3
141-
}
142-
return self.preferredFont.withRelativeSize(relativePadding)
143-
}
144-
}
145-
}
146-
147-
// MARK: - Layout Helpers
148-
149-
extension InputFieldVM {
150-
var inputFieldTopPadding: CGFloat {
151-
switch self.size {
152-
case .small: 30
153-
case .medium: 34
154-
case .large: 38
155-
}
156-
}
157-
var inputFieldHeight: CGFloat {
158-
switch self.size {
159-
case .small: 26
160-
case .medium: 28
161-
case .large: 30
162-
}
163-
}
164-
var verticalPadding: CGFloat {
165-
switch self.size {
166-
case .small: 12
167-
case .medium: 14
168-
case .large: 16
169-
}
170-
}
171-
var height: CGFloat {
172-
return switch self.size {
173-
case .small: 40
174-
case .medium: 60
175-
case .large: 80
176-
}
177-
}
178131
}
179132

180133
// MARK: - UIKit Helpers
@@ -189,17 +142,17 @@ extension InputFieldVM {
189142
.foregroundColor: self.placeholderColor.uiColor
190143
])
191144
}
192-
func nsAttributedTitle(for position: InputFieldTitlePosition) -> NSAttributedString {
145+
var nsAttributedTitle: NSAttributedString? {
193146
guard let title else {
194-
return NSAttributedString()
147+
return nil
195148
}
196149

197150
let attributedString = NSMutableAttributedString()
198151
attributedString.append(NSAttributedString(
199152
string: title,
200153
attributes: [
201-
.font: self.titleFont(for: position).uiFont,
202-
.foregroundColor: self.titleColor(for: position).uiColor
154+
.font: self.preferredFont.uiFont,
155+
.foregroundColor: self.foregroundColor.uiColor
203156
]
204157
))
205158
if self.isRequired {
@@ -212,7 +165,7 @@ extension InputFieldVM {
212165
attributedString.append(NSAttributedString(
213166
string: "*",
214167
attributes: [
215-
.font: self.titleFont(for: position).uiFont,
168+
.font: self.preferredFont.uiFont,
216169
.foregroundColor: UniversalColor.danger.uiColor
217170
]
218171
))
@@ -222,40 +175,22 @@ extension InputFieldVM {
222175
func shouldUpdateLayout(_ oldModel: Self) -> Bool {
223176
return self.size != oldModel.size
224177
|| self.horizontalPadding != oldModel.horizontalPadding
178+
|| self.spacing != oldModel.spacing
179+
|| self.cornerRadius != oldModel.cornerRadius
225180
}
226181
}
227182

228-
// MARK: - UIKit Helpers
183+
// MARK: - SwiftUI Helpers
229184

230185
extension InputFieldVM {
231186
var autocorrectionType: UITextAutocorrectionType {
232187
return self.isAutocorrectionEnabled ? .yes : .no
233188
}
234-
func attributedTitle(
235-
for position: InputFieldTitlePosition
236-
) -> AttributedString {
237-
guard let title else {
238-
return AttributedString()
239-
}
240-
241-
var attributedString = AttributedString()
242-
243-
var attributedTitle = AttributedString(title)
244-
attributedTitle.font = self.titleFont(for: position).font
245-
attributedTitle.foregroundColor = self.titleColor(for: position).uiColor
246-
attributedString.append(attributedTitle)
247-
248-
if self.isRequired {
249-
var space = AttributedString(" ")
250-
space.font = .systemFont(ofSize: 5)
251-
attributedString.append(space)
252-
253-
var requiredSign = AttributedString("*")
254-
requiredSign.font = self.titleFont(for: position).font
255-
requiredSign.foregroundColor = UniversalColor.danger.uiColor
256-
attributedString.append(requiredSign)
189+
var attributedTitle: AttributedString? {
190+
guard let nsAttributedTitle else {
191+
return nil
257192
}
258193

259-
return attributedString
194+
return AttributedString(nsAttributedTitle)
260195
}
261196
}

Sources/SwiftComponents/InputField/SUInputField.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public struct SUInputField<FocusValue: Hashable>: View {
5151

5252
public var body: some View {
5353
HStack(spacing: self.model.spacing) {
54-
if self.model.title.isNotNilAndEmpty {
55-
Text(self.model.attributedTitle(for: .center))
54+
if let title = self.model.attributedTitle {
55+
Text(title)
5656
.font(self.model.preferredFont.font)
5757
.foregroundStyle(
5858
self.model.foregroundColor.color(for: self.colorScheme)

0 commit comments

Comments
 (0)