-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFloatingTextField.swift
207 lines (162 loc) · 6.59 KB
/
FloatingTextField.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// FloatingTextField.swift
//
// Created by Abhishek.Rathi on 25/04/19.
// Copyright © 2019 Abhishek.Rathi. All rights reserved.
//
import UIKit
// add new style here, for more customization according to your app design.
enum TextFieldStyle {
case green
case red
case gray
}
@IBDesignable class FloatingTextField: UITextField {
@IBInspectable var highlightColour: UIColor? = .gray
var noInputHighlightColor: UIColor? = .red
let placeholderLabel = UILabel()
let bottomLayer = CAShapeLayer()
lazy var strokeEndAnimation: CABasicAnimation = {
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.toValue = 1
animation.fillMode = .forwards
animation.isRemovedOnCompletion = false
return animation
}()
lazy var strokeStartAnimation: CABasicAnimation = {
let animation = CABasicAnimation(keyPath: "strokeStart")
animation.toValue = 0
animation.fillMode = .forwards
animation.isRemovedOnCompletion = false
return animation
}()
override var text: String?{
willSet{
self.setStyle(style: .gray)
self.animatePlaceholderLabelUp()
}
}
override func awakeFromNib() {
setupPlaceholderLabel()
animatePlaceHolderLabelDown()
}
fileprivate func setupPlaceholderLabel() {
addSubview(placeholderLabel)
self.delegate = self
// need these to work animate properly
self.placeholderLabel.minimumScaleFactor = 0.3
self.placeholderLabel.adjustsFontSizeToFitWidth = true
self.placeholderLabel.lineBreakMode = .byClipping
self.placeholderLabel.numberOfLines = 0
self.placeholderLabel.text = self.placeholder
self.placeholder?.removeAll()
//Bottom Line CAlayer name
bottomLayer.name = "bottomLine"
// Change colour
self.placeholderLabel.font = self.font
self.placeholderLabel.textColor = .lightGray
self.borderStyle = .none
self.setBottomBorder(color: .lightGray)
}
// MARK:- Use this function to set default colour of Floating label's text- Can be customized.
func setStyle(style: TextFieldStyle) {
layoutIfNeeded()
switch style {
case .green:
self.setBottomBorder(color: #colorLiteral(red: 0.1093588177, green: 0.5, blue: 0.1868519905, alpha: 1))
animatePlaceholderLabelUp()
case .gray:
self.setBottomBorder(color: .lightGray)
self.placeholderLabel.textColor = .lightGray
case .red:
self.setBottomBorder(color: #colorLiteral(red: 1, green: 0.1511251674, blue: 0.08996533756, alpha: 0.8503050086))
self.placeholderLabel.textColor = .red
}
}
func showWrongInput() {
if noInputHighlightColor != nil {
self.setBottomBorder(color: noInputHighlightColor!)
self.placeholderLabel.textColor = noInputHighlightColor!
self.placeholderLabel.alpha = 0.8
}
else{
setStyle(style: .red)
}
}
}
// MARK:- TextField delegate functions.
extension FloatingTextField : UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
if !Thread.isMainThread {
print("Called from background thread")
return
}
self.setBottomBorder(color: highlightColour!)
animatePlaceholderLabelUp()
}
func textFieldDidEndEditing(_ textField: UITextField) {
self.setStyle(style: .gray)
if textField.text == "" {
if noInputHighlightColor != nil {
self.setBottomBorder(color: noInputHighlightColor!)
}
else{
self.setBottomBorder(color: .red)
}
animatePlaceHolderLabelDown()
}
}
}
// MARK:- Floating Label Animation functions.
extension FloatingTextField {
fileprivate func setBottomBorder(color: UIColor) {
let existingBottomLayer = layer.sublayers?.filter({ layer in
layer.name == bottomLayer.name })
if (existingBottomLayer?.count)! > 0 {
guard let bottomShapelayer = existingBottomLayer![0] as? CAShapeLayer else { return }
bottomShapelayer.strokeColor = color.cgColor
}
else {
let bezPath = UIBezierPath()
bezPath.move(to: CGPoint(x: bounds.minX, y: bounds.maxY + 1))
bezPath.addLine(to: CGPoint(x: bounds.maxX, y: bounds.maxY + 1))
bottomLayer.path = bezPath.cgPath
bottomLayer.opacity = 0.8
bottomLayer.lineCap = .round
bottomLayer.strokeColor = color.cgColor
bottomLayer.lineWidth = 0.6
bottomLayer.strokeStart = 0.5
bottomLayer.strokeEnd = 0.5
let animationGroup = CAAnimationGroup()
animationGroup.duration = 0.5
animationGroup.animations = [strokeStartAnimation,strokeEndAnimation]
animationGroup.isRemovedOnCompletion = false
animationGroup.fillMode = .forwards
bottomLayer.add(animationGroup, forKey: "strokeAnimation")
layer.addSublayer(bottomLayer)
}
}
fileprivate func animatePlaceholderLabelUp() {
let heightSmall = self.bounds.height * 0.5
DispatchQueue.main.async {
UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {
self.placeholderLabel.frame = CGRect(x: 0,
y: self.bounds.minY - heightSmall,
width: self.bounds.width,
height: heightSmall )
self.placeholderLabel.textColor = self.highlightColour
})
}
}
fileprivate func animatePlaceHolderLabelDown() {
let heightlarge = self.bounds.height * 0.95
DispatchQueue.main.async {
UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {
self.placeholderLabel.frame = CGRect(x: 0,
y: 0,
width: self.bounds.width,
height: heightlarge)
})
}
}
}