-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFloatingTextField.swift
159 lines (120 loc) · 3.83 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
//
// FloatingTextField.swift
// AnimatedTextField
//
// Created by Alexey Zhulikov on 22.10.2019.
// Copyright © 2019 Alexey Zhulikov. All rights reserved.
//
import UIKit
private extension TimeInterval {
static let animation250ms: TimeInterval = 0.25
}
private extension UIColor {
static let inactive: UIColor = .gray
}
private enum Constants {
static let offset: CGFloat = 8
static let placeholderSize: CGFloat = 14
}
final class FloatingTextField: UITextField {
// MARK: - Subviews
private var border = UIView()
private var label = UILabel()
// MARK: - Private Properties
private var scale: CGFloat {
Constants.placeholderSize / fontSize
}
private var fontSize: CGFloat {
font?.pointSize ?? 0
}
private var labelHeight: CGFloat {
ceil(font?.withSize(Constants.placeholderSize).lineHeight ?? 0)
}
private var textHeight: CGFloat {
ceil(font?.lineHeight ?? 0)
}
private var isEmpty: Bool {
text?.isEmpty ?? true
}
private var textInsets: UIEdgeInsets {
UIEdgeInsets(top: Constants.offset + labelHeight, left: 0, bottom: Constants.offset, right: 0)
}
// MARK: - Initialization
override init(frame: CGRect) {
super.init(frame: frame)
setupUI()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupUI()
}
// MARK: - UITextField
override var intrinsicContentSize: CGSize {
return CGSize(width: bounds.width, height: textInsets.top + textHeight + textInsets.bottom)
}
override var placeholder: String? {
didSet {
label.text = placeholder
}
}
override func layoutSubviews() {
super.layoutSubviews()
border.frame = CGRect(x: 0, y: bounds.height - 1, width: bounds.width, height: 1)
updateLabel(animated: false)
}
override func textRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: textInsets)
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: textInsets)
}
override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
return .zero
}
override func draw(_ rect: CGRect) {
super.draw(rect)
guard !isFirstResponder else {
return
}
label.transform = .identity
label.frame = bounds.inset(by: textInsets)
}
// MARK: - Private Methods
private func setupUI() {
borderStyle = .none
border.backgroundColor = .inactive
border.isUserInteractionEnabled = false
addSubview(border)
label.textColor = .inactive
label.font = font
label.text = placeholder
label.isUserInteractionEnabled = false
addSubview(label)
addTarget(self, action: #selector(handleEditing), for: .allEditingEvents)
}
@objc
private func handleEditing() {
updateLabel()
updateBorder()
}
private func updateBorder() {
let borderColor = isFirstResponder ? tintColor : .inactive
UIView.animate(withDuration: .animation250ms) {
self.border.backgroundColor = borderColor
}
}
private func updateLabel(animated: Bool = true) {
let isActive = isFirstResponder || !isEmpty
let offsetX = -label.bounds.width * (1 - scale) / 2
let offsetY = -label.bounds.height * (1 - scale) / 2
let transform = CGAffineTransform(translationX: offsetX, y: offsetY - labelHeight - Constants.offset)
.scaledBy(x: scale, y: scale)
guard animated else {
label.transform = isActive ? transform : .identity
return
}
UIView.animate(withDuration: .animation250ms) {
self.label.transform = isActive ? transform : .identity
}
}
}