Skip to content

Commit 6ae2dbb

Browse files
committed
Update code and added new properties
1 parent 2b52de9 commit 6ae2dbb

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

Example/FloatingLabelTextFieldSwiftUI/ContentView.swift

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import FloatingLabelTextFieldSwiftUI
1212
struct ContentView: View {
1313

1414
@State private var firstName: String = ""
15+
@State private var isValidFirstName: Bool = false
16+
1517
@State private var lastName: String = ""
18+
@State private var isValidLastName: Bool = false
19+
1620
@State private var mobileNumber: String = ""
1721
@State private var email: String = ""
1822
@State private var isValidEmail: Bool = false
@@ -41,7 +45,7 @@ struct ContentView: View {
4145
VStack {
4246

4347
HStack(spacing: 20) {
44-
FloatingLabelTextField($firstName, placeholder: "First Name", editingChanged: { (isChanged) in
48+
FloatingLabelTextField($firstName, validtionChecker: $isValidFirstName, placeholder: "First Name", editingChanged: { (isChanged) in
4549

4650
}) {
4751

@@ -56,7 +60,7 @@ struct ContentView: View {
5660
.modifier(ThemeTextField())
5761

5862

59-
FloatingLabelTextField($lastName, placeholder: "Last Name", editingChanged: { (isChanged) in
63+
FloatingLabelTextField($lastName, validtionChecker: $isValidLastName, placeholder: "Last Name", editingChanged: { (isChanged) in
6064

6165
}) {
6266

@@ -121,12 +125,15 @@ struct ContentView: View {
121125
})
122126
.isSecureTextEntry(!self.isPasswordShow)
123127
.modifier(ThemeTextField())
124-
// SecureField("", text: $password)
125-
// Text(password)
128+
126129
Button(action: {
127130
self.endEditing(true)
128131

129-
if self.isValidEmail {
132+
print("First Name--->", isValidFirstName)
133+
print("Last Name--->", isValidLastName)
134+
print("Email--->", isValidEmail)
135+
136+
if self.isValidEmail && isValidFirstName && isValidLastName {
130137
print("Valid field")
131138

132139
} else {

Sources/FloatingLabelTextFieldSwiftUI/FloatingLabelTextField.swift

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public struct FloatingLabelTextField: View {
3636

3737
@State fileprivate var isShowError: Bool = false
3838

39+
@State fileprivate var isFocused: Bool = false
40+
3941
//MARK: Observed Object
4042
@ObservedObject private var notifier = FloatingLabelTextFieldNotifier()
4143

@@ -57,7 +59,7 @@ public struct FloatingLabelTextField: View {
5759
var centerTextFieldView: some View {
5860
ZStack(alignment: notifier.textAlignment.getAlignment()) {
5961

60-
if textFieldValue.isEmpty {
62+
if (notifier.isAnimateOnFocus ? !isSelected : textFieldValue.isEmpty) {
6163
Text(placeholderText)
6264
.font(notifier.placeholderFont)
6365
.multilineTextAlignment(notifier.textAlignment)
@@ -77,22 +79,31 @@ public struct FloatingLabelTextField: View {
7779
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
7880
if let currentResponder = UIResponder.currentFirstResponder, let currentTextField = currentResponder.globalView as? UITextField{
7981
arrTextFieldEditActions = self.notifier.arrTextFieldEditActions
80-
self.isSelected = self.notifier.isSecureTextEntry
82+
withAnimation {
83+
self.isSelected = self.notifier.isSecureTextEntry
84+
}
8185
currentTextField.addAction(for: .editingDidEnd) {
8286
self.isSelected = false
8387
self.isShowError = self.notifier.isRequiredField
88+
self.validtionChecker = self.currentError.condition
8489
self.commit()
90+
arrTextFieldEditActions = []
8591
}
8692
}
8793
}
8894
}
95+
.disabled(self.notifier.disabled)
96+
.allowsHitTesting(self.notifier.allowsHitTesting)
8997
.font(notifier.font)
9098
.multilineTextAlignment(notifier.textAlignment)
9199
.foregroundColor((self.currentError.condition || !notifier.isShowError) ? (isSelected ? notifier.selectedTextColor : notifier.textColor) : notifier.errorColor)
92100

93101
} else {
94102
TextField("", text: $textFieldValue.animation(), onEditingChanged: { (isChanged) in
95-
self.isSelected = isChanged
103+
withAnimation {
104+
self.isSelected = isChanged
105+
}
106+
96107
self.validtionChecker = self.currentError.condition
97108
self.editingChanged(isChanged)
98109
self.isShowError = self.notifier.isRequiredField
@@ -101,7 +112,10 @@ public struct FloatingLabelTextField: View {
101112
self.isShowError = self.notifier.isRequiredField
102113
self.validtionChecker = self.currentError.condition
103114
self.commit()
115+
arrTextFieldEditActions = []
104116
})
117+
.disabled(self.notifier.disabled)
118+
.allowsHitTesting(self.notifier.allowsHitTesting)
105119
.multilineTextAlignment(notifier.textAlignment)
106120
.font(notifier.font)
107121
.foregroundColor((self.currentError.condition || !notifier.isShowError) ? (isSelected ? notifier.selectedTextColor : notifier.textColor) : notifier.errorColor)
@@ -130,7 +144,7 @@ public struct FloatingLabelTextField: View {
130144
ZStack(alignment: .bottomLeading) {
131145

132146
//Top error and title lable view
133-
if notifier.isShowError && self.isShowError && textFieldValue.isEmpty {
147+
if notifier.isShowError && self.isShowError && textFieldValue.isEmpty || (notifier.isAnimateOnFocus && isSelected){
134148
self.topTitleLable.padding(.bottom, CGFloat(notifier.spaceBetweenTitleText)).opacity(1)
135149

136150
} else {
@@ -202,6 +216,18 @@ extension FloatingLabelTextField {
202216
notifier.isSecureTextEntry = isSecure
203217
return self
204218
}
219+
220+
/// Whether users can interact with this.
221+
public func disabled(_ isDisabled: Bool) -> Self {
222+
notifier.disabled = isDisabled
223+
return self
224+
}
225+
226+
/// Whether this view participates in hit test operations.
227+
public func allowsHitTesting(_ isAllowsHitTesting: Bool) -> Self {
228+
notifier.allowsHitTesting = isAllowsHitTesting
229+
return self
230+
}
205231
}
206232

207233
//MARK: Line Property Funcation
@@ -342,3 +368,14 @@ extension FloatingLabelTextField {
342368
return self
343369
}
344370
}
371+
372+
//MARK: Animation Style Funcation
373+
@available(iOS 13.0, *)
374+
extension FloatingLabelTextField {
375+
/// Enable the placeholder label when the textfield is focused.
376+
public func enablePlaceholderOnFocus(_ isEanble: Bool) -> Self {
377+
notifier.isAnimateOnFocus = isEanble
378+
return self
379+
}
380+
}
381+

Sources/FloatingLabelTextFieldSwiftUI/ObservableObject/ObservableObject.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class FloatingLabelTextFieldNotifier: ObservableObject {
4141
//MARK: Other Properties
4242
@Published var spaceBetweenTitleText: Double = 15
4343
@Published var isSecureTextEntry: Bool = false
44+
@Published var disabled: Bool = false
45+
@Published var allowsHitTesting: Bool = true
4446

4547
//MARK: Error Properties
4648
@Published var isShowError: Bool = false
@@ -51,4 +53,7 @@ class FloatingLabelTextFieldNotifier: ObservableObject {
5153

5254
//MARK: Action Editing Properties
5355
@Published var arrTextFieldEditActions: [TextFieldEditActions] = []
56+
57+
//MARK: Animation Style Properties
58+
@Published var isAnimateOnFocus: Bool = false
5459
}

0 commit comments

Comments
 (0)