-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathJDTextField.swift
More file actions
268 lines (212 loc) · 9.33 KB
/
JDTextField.swift
File metadata and controls
268 lines (212 loc) · 9.33 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//
// AnimatedTextField.swift
// CustomTextField
//
// Created by Jawad Ali on 26/09/2021.
//
import UIKit
@IBDesignable
open class JDTextField: UITextField {
//MARK:- Views
private lazy var shapeLayer = CAShapeLayerFactory.createShapeLayer()
private lazy var shapeLayerRight = CAShapeLayerFactory.createShapeLayer()
private lazy var shapeLayerLeft = CAShapeLayerFactory.createShapeLayer()
@IBInspectable dynamic open var shapeColor = UIColor.red.withAlphaComponent(0.3) { didSet { layoutIfNeeded() } }
@IBInspectable dynamic open var placeHolderFont: UIFont = UIFont.boldSystemFont(ofSize: 20) { didSet { layoutIfNeeded() } }
@IBInspectable dynamic open var errorLabelFont: UIFont = UIFont.boldSystemFont(ofSize: 14) { didSet { layoutIfNeeded() } }
@IBInspectable dynamic open var errorLabelColor: UIColor = .red { didSet { layoutIfNeeded() } }
@IBInspectable dynamic open var lineWidth: CGFloat = 4 { didSet { layoutIfNeeded() } }
public enum ShapeType {
case circular
case square
}
private lazy var floatingLabel: UILabel = {
let label = UILabel()
label.text = placeholder?.uppercased()
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
private lazy var errorLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
open override var placeholder: String? { didSet {
floatingLabel.text = placeholder?.uppercased()
layoutIfNeeded() }
}
private var leftLabelConstraint: NSLayoutConstraint!
private var centerLabelConstraint: NSLayoutConstraint!
private var topLabelConstraint: NSLayoutConstraint!
private var heightErrorLabelConstraint: NSLayoutConstraint!
private var topErrorLabelConstraint: NSLayoutConstraint!
private let offset: CGFloat = 30
private let topOffset: CGFloat = 6
private let speed: Double = 0.2
private var shapeType: ShapeType = .circular
private lazy var allLayers = [shapeLayer, shapeLayerLeft, shapeLayerRight]
//MARK:- init
public init(type: ShapeType) {
super.init(frame: .zero)
shapeType = type
commonInit()
}
public override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required public init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
public override var intrinsicContentSize: CGSize {
return CGSize(width: super.intrinsicContentSize.width, height: 70)
}
public override func layoutSubviews() {
super.layoutSubviews()
updatePaths()
setColors()
setFonts()
setLineWidth()
}
}
private extension JDTextField {
/// A problem appeard when I add extra option to the field to hide or show user's inputs (isSecureTextEntry).
/// When 'isSecureTextEntry == true' on the first field and I try to switch on another app just block UI and its seems
/// like deadlock is happened. I found that program stops on 97 line.
/// After commenting a line problem was resolved for me and app continued working well as expected.
func setColors() {
allLayers.forEach { $0.strokeColor = shapeColor.cgColor }
// >>> potential deadlock >>> self.textColor = tintColor
floatingLabel.textColor = isFirstResponder ? tintColor : shapeColor
errorLabel.textColor = errorLabelColor
}
func setFonts() {
floatingLabel.font = placeHolderFont
errorLabel.font = errorLabelFont
}
func setLineWidth() {
allLayers.forEach { $0.lineWidth = lineWidth }
}
func commonInit() {
self.borderStyle = .none
self.font = UIFont.systemFont(ofSize: 20)
setupView()
addConstraints()
self.addTarget(self, action: #selector(becomeResponder), for: .editingDidBegin)
self.addTarget(self, action: #selector(resignResponder), for: .editingDidEnd)
}
func setupView() {
[shapeLayer, shapeLayerLeft, shapeLayerRight].forEach(layer.addSublayer)
addSubview(floatingLabel)
addSubview(errorLabel)
}
func addConstraints() {
leftLabelConstraint = floatingLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: offset)
centerLabelConstraint = floatingLabel.centerYAnchor.constraint(equalTo: centerYAnchor)
topLabelConstraint = floatingLabel.topAnchor.constraint(equalTo: topAnchor)
topErrorLabelConstraint = errorLabel.topAnchor.constraint(equalTo: topAnchor)
heightErrorLabelConstraint = errorLabel.heightAnchor.constraint(equalToConstant: 0)
NSLayoutConstraint.activate ( [
leftLabelConstraint,
centerLabelConstraint,
floatingLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -offset),
errorLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: offset),
errorLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -offset),
topErrorLabelConstraint,
heightErrorLabelConstraint
])
}
func updatePaths() {
topErrorLabelConstraint.constant = bounds.maxY
let placeholderWidth = floatingLabel.text?.width(withConstrainedHeight: .infinity, font: floatingLabel.font)
let halfLineWidth = lineWidth/2
let bezierPath = UIBezierPath()
bezierPath.move(to: CGPoint(x: bounds.minX + offset + halfLineWidth, y: bounds.maxY-lineWidth/2))
bezierPath.addLine(to: CGPoint(x: bounds.maxX - offset - halfLineWidth, y: bounds.maxY-lineWidth/2))
shapeLayer.path = bezierPath.cgPath
if shapeType == .circular {
(shapeLayerLeft.path, shapeLayerRight.path) = circularShape(halfLineWidth: halfLineWidth, placeholderWidth: placeholderWidth)
} else {
(shapeLayerLeft.path, shapeLayerRight.path) = squareShape(halfLineWidth: halfLineWidth, placeholderWidth: placeholderWidth)
}
shapeLayerRight.strokeEnd = 0
shapeLayerLeft.strokeEnd = 0
}
func squareShape(halfLineWidth: CGFloat, placeholderWidth: CGFloat?) -> (CGPath, CGPath) {
let bezierPathRight = UIBezierPath()
bezierPathRight.move(to: CGPoint(x: bounds.maxX - offset + halfLineWidth, y: bounds.maxY - halfLineWidth))
bezierPathRight.addLine(to: CGPoint(x: bounds.maxX - halfLineWidth, y: bounds.maxY - halfLineWidth))
bezierPathRight.addLine(to: CGPoint(x: bounds.maxX - halfLineWidth, y: topOffset + lineWidth*2))
bezierPathRight.addLine(to: CGPoint(x: offset + (placeholderWidth ?? 0.0), y: bezierPathRight.currentPoint.y))
let bezierPathLeft = UIBezierPath()
bezierPathLeft.move(to: CGPoint(x: offset - halfLineWidth, y: bounds.maxY - halfLineWidth))
bezierPathLeft.addLine(to: CGPoint(x: .leastNormalMagnitude, y: bounds.maxY - halfLineWidth))
bezierPathLeft.addLine(to: CGPoint(x: .leastNormalMagnitude, y: topOffset + lineWidth*2))
bezierPathLeft.addLine(to: CGPoint(x: offset - halfLineWidth, y: topOffset + lineWidth*2))
return (bezierPathLeft.cgPath, bezierPathRight.cgPath)
}
func circularShape(halfLineWidth: CGFloat, placeholderWidth: CGFloat?) -> (CGPath, CGPath) {
let bezierPathRight = UIBezierPath()
bezierPathRight.addArc(withCenter: CGPoint(x: bounds.maxX - offset + halfLineWidth, y: bounds.midY + topOffset), radius: bounds.midY-topOffset - halfLineWidth, startAngle: 90.deg2rad , endAngle: -90.deg2rad, clockwise: false)
bezierPathRight.addLine(to: CGPoint(x: offset + (placeholderWidth ?? 0.0), y: bezierPathRight.currentPoint.y))
let bezierPathLeft = UIBezierPath()
bezierPathLeft.addArc(withCenter: CGPoint(x: offset - halfLineWidth, y: bounds.midY + topOffset), radius: bounds.midY - halfLineWidth - topOffset, startAngle: 90.deg2rad, endAngle: 270.deg2rad, clockwise: true)
return (bezierPathLeft.cgPath, bezierPathRight.cgPath)
}
}
// MARK: - Actions on textfield responder and resign
private extension JDTextField {
@objc func becomeResponder() {
guard let text = text, text.isEmpty else {
return
}
shapeLayerLeft.strokeAnimation(duration: speed, from: 0, to: 1)
shapeLayerRight.strokeAnimation(duration: speed, from: 0, to: 1)
leftLabelConstraint.constant = offset + 10
centerLabelConstraint.isActive = false
topLabelConstraint.isActive = true
UIView.animate(withDuration: speed) {
self.layoutIfNeeded()
}
}
@objc func resignResponder() {
guard let text = text, text.isEmpty else {
return
}
shapeLayerLeft.strokeAnimation(duration: speed, from: 1, to: 0)
shapeLayerRight.strokeAnimation(duration: speed, from: 1, to: 0)
leftLabelConstraint.constant = offset
centerLabelConstraint.isActive = true
topLabelConstraint.isActive = false
UIView.animate(withDuration: speed) {
self.layoutIfNeeded()
}
}
}
// MARK: - TextField override rect
extension JDTextField {
open override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
return .zero
}
open override func editingRect(forBounds bounds: CGRect) -> CGRect {
return adjustedTextBounds(super.editingRect(forBounds: bounds))
}
open override func textRect(forBounds bounds: CGRect) -> CGRect {
return adjustedTextBounds(super.textRect(forBounds: bounds))
}
private func adjustedTextBounds(_ bounds: CGRect) -> CGRect {
CGRect(x: bounds.origin.x + offset, y: bounds.origin.y + topOffset*2, width: bounds.size.width - offset*2, height: bounds.size.height - topOffset*2)
}
}
public extension JDTextField {
func showError(with message: String) {
heightErrorLabelConstraint.constant = 30
errorLabel.text = message
errorLabel.shake()
}
func hideError() {
heightErrorLabelConstraint.constant = 0
errorLabel.text = nil
}
}