-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSloyTextField.swift
145 lines (112 loc) · 3.72 KB
/
SloyTextField.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
//
// SloyTextField.swift
// AnimatedTextField
//
// Created by Alexey Zhulikov on 20.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 SloyTextField: UITextField {
// MARK: - Subviews
private let border = UIView()
private let label = UILabel()
// MARK: - Private Properties
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 func layoutSubviews() {
super.layoutSubviews()
border.frame = CGRect(x: 0, y: bounds.height - 1, width: bounds.width, height: 1)
updateLabel(animated: false)
}
override var intrinsicContentSize: CGSize {
return CGSize(width: bounds.width, height: textInsets.top + textHeight + textInsets.bottom)
}
override var placeholder: String? {
didSet {
label.text = placeholder
attributedPlaceholder = NSAttributedString(string: placeholder ?? "", attributes: [
NSAttributedString.Key.foregroundColor: UIColor.inactive
])
}
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: textInsets)
}
override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: textInsets)
}
override func textRect(forBounds bounds: CGRect) -> CGRect {
return 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?.withSize(Constants.placeholderSize)
label.text = placeholder
label.isUserInteractionEnabled = false
addSubview(label)
attributedPlaceholder = NSAttributedString(string: placeholder ?? "", attributes: [
NSAttributedString.Key.foregroundColor: UIColor.inactive
])
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 alpha: CGFloat = isEmpty ? 0 : 1
let y = isEmpty ? labelHeight * 0.5 : 0
let labelFrame = CGRect(x: 0, y: y, width: bounds.width, height: labelHeight)
guard animated else {
label.frame = labelFrame
label.alpha = alpha
return
}
UIView.animate(withDuration: .animation250ms) {
self.label.frame = labelFrame
self.label.alpha = alpha
}
}
}