@@ -78,6 +78,10 @@ public class CocoaTextField: UITextField {
7878 get { return hintLabel. text }
7979 }
8080
81+ public override var text : String ? {
82+ didSet { updateHint ( ) }
83+ }
84+
8185 private var isHintVisible = false
8286 private let hintLabel = UILabel ( )
8387 private let errorLabel = UILabel ( )
@@ -121,11 +125,27 @@ public class CocoaTextField: UITextField {
121125 }
122126
123127 private func configureHint( ) {
124- hintLabel. transform = CGAffineTransform . identity. translatedBy ( x: padding, y: 0 )
125128 hintLabel. autoresizingMask = [ . flexibleWidth, . flexibleHeight]
126- hintLabel . font = font
129+ self . updateHint ( )
127130 hintLabel. textColor = inactiveHintColor
128131 }
132+
133+ private func updateHint( ) {
134+ if isHintVisible {
135+ // Small placeholder
136+ self . hintLabel. alpha = 1
137+ self . hintLabel. transform = CGAffineTransform . identity. translatedBy ( x: self . padding, y: - self . hintHeight ( ) )
138+ self . hintLabel. font = self . hintFont
139+ } else if self . text? . isEmpty ?? true {
140+ // Large placeholder
141+ self . hintLabel. alpha = 1
142+ self . hintLabel. transform = CGAffineTransform . identity. translatedBy ( x: self . padding, y: 0 )
143+ self . hintLabel. font = self . font
144+ } else {
145+ // No placeholder
146+ self . hintLabel. alpha = 0
147+ }
148+ }
129149
130150 private func configureErrorLabel( ) {
131151 errorLabel. font = UIFont . systemFont ( ofSize: 12 )
@@ -137,42 +157,28 @@ public class CocoaTextField: UITextField {
137157
138158 private func activateTextField( ) {
139159 if isHintVisible { return }
160+ isHintVisible. toggle ( )
140161
141162 UIView . animate ( withDuration: 0.2 ) {
142- if let text = self . text, !text. isEmpty {
143- self . hintLabel. alpha = 1
144- } else {
145- self . hintLabel. transform =
146- CGAffineTransform . identity. translatedBy ( x: self . padding, y: - self . hintHeight ( ) )
147- self . hintLabel. font = self . hintFont
148- }
163+ self . updateHint ( )
149164 self . hintLabel. textColor = self . activeHintColor
150165 self . backgroundColor = self . focusedBackgroundColor
151166 if self . errorLabel. alpha == 0 {
152167 self . layer. borderColor = self . focusedBackgroundColor. cgColor
153168 }
154169 }
155-
156- isHintVisible. toggle ( )
157170 }
158171
159172 private func deactivateTextField( ) {
160173 if !isHintVisible { return }
174+ isHintVisible. toggle ( )
161175
162176 UIView . animate ( withDuration: 0.3 ) {
163- if let text = self . text, !text. isEmpty {
164- self . hintLabel. alpha = 0
165- } else {
166- self . hintLabel. transform =
167- CGAffineTransform . identity. translatedBy ( x: self . padding, y: 0 )
168- self . hintLabel. font = self . font
169- }
177+ self . updateHint ( )
170178 self . hintLabel. textColor = self . inactiveHintColor
171179 self . backgroundColor = self . defaultBackgroundColor
172180 self . layer. borderColor = self . borderColor. cgColor
173181 }
174-
175- isHintVisible. toggle ( )
176182 }
177183
178184 private func hintHeight( ) -> CGFloat {
0 commit comments