@@ -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+
0 commit comments