-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathPinCodeInputView.swift
More file actions
239 lines (185 loc) · 6.79 KB
/
Copy pathPinCodeInputView.swift
File metadata and controls
239 lines (185 loc) · 6.79 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
//
// PinCodeInputView.swift
// PinCodeInputView
//
// Created by Jinsei Shima on 2018/11/06.
// Copyright © 2018 Jinsei Shima. All rights reserved.
//
import UIKit
@IBDesignable
public class PinCodeInputView<T: UIView & ItemType>: UIControl, UITextInputTraits, UIKeyInput {
// MARK: - Properties
private(set) public var text: String = "" {
didSet {
if let handler = changeTextHandler {
handler(text)
}
updateText()
}
}
public var isEmpty: Bool {
return text.isEmpty
}
public var isFilled: Bool {
return text.count == digit
}
public var hasText: Bool {
return !(text.isEmpty)
}
override public var intrinsicContentSize: CGSize {
return stackView.bounds.size
}
private let digit: Int
private let itemSpacing: CGFloat
private var changeTextHandler: ((String) -> Void)? = nil
private let stackView: UIStackView = .init()
private var items: [ContainerItemView<T>] = []
private let itemFactory: () -> UIView
private var appearance: ItemAppearance?
private let autoResizes: Bool
// MARK: - UITextInputTraits
public var autocapitalizationType = UITextAutocapitalizationType.none
public var autocorrectionType = UITextAutocorrectionType.no
public var spellCheckingType = UITextSpellCheckingType.no
public var keyboardType = UIKeyboardType.numberPad
public var keyboardAppearance = UIKeyboardAppearance.default
public var returnKeyType = UIReturnKeyType.done
public var enablesReturnKeyAutomatically = true
// MARK: - Initializers
public init(
digit: Int,
itemSpacing: CGFloat,
itemFactory: @escaping (() -> T),
autoresizes: Bool = false) {
self.digit = digit
self.itemSpacing = itemSpacing
self.itemFactory = itemFactory
self.autoResizes = autoresizes
super.init(frame: .zero)
self.items = (0..<digit).map { _ in
let item = ContainerItemView(itemView: itemFactory())
item.setHandler {
self.showCursor()
self.becomeFirstResponder()
}
return item
}
self.stackView.frame = self.bounds
addSubview(stackView)
items.forEach { stackView.addArrangedSubview($0) }
stackView.spacing = itemSpacing
stackView.axis = .horizontal
stackView.distribution = .fillEqually
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Functions
override public func layoutSubviews() {
super.layoutSubviews()
guard let appearance = appearance else {
stackView.frame = bounds
return
}
stackView.bounds = CGRect(
x: 0,
y: 0,
width: (appearance.itemSize.width * CGFloat(digit)) + (itemSpacing * CGFloat(digit - 1)),
height: appearance.itemSize.height
)
stackView.center = CGPoint(x: bounds.width / 2, y: bounds.height / 2)
}
public func set(text: String) {
if Validator.isPinCode(text: text, digit: digit) {
self.text = text
}
}
public func set(changeTextHandler: @escaping (String) -> ()) {
self.changeTextHandler = changeTextHandler
}
public func set(appearance: ItemAppearance) {
self.appearance = appearance
if autoResizes {
self.appearance = ItemAppearance(itemSize: CGSize(width: (self.bounds.width - (self.itemSpacing * CGFloat(self.digit))) / CGFloat(self.digit), height: appearance.itemSize.height), font: appearance.font, textColor: appearance.textColor, backgroundColor: appearance.backgroundColor, cursorColor: appearance.cursorColor, cornerRadius: appearance.cornerRadius, borderColor: appearance.borderColor)
}
items.forEach { $0.itemView.set(appearance: appearance) }
}
private func updateText() {
items.enumerated().forEach { (index, item) in
if (0..<text.count).contains(index) {
let _index = text.index(text.startIndex, offsetBy: index)
item.itemView.text = text[_index]
} else {
item.itemView.text = nil
}
}
showCursor()
}
private func showCursor() {
let cursorPosition = text.count
items.enumerated().forEach { (arg) in
let (index, item) = arg
item.itemView.isHiddenCursor = (index == cursorPosition) ? false : true
}
}
private func hiddenCursor() {
items.forEach { $0.itemView.isHiddenCursor = true }
}
// MARK: - UIKeyInput
public func insertText(_ textToInsert: String) {
if isEnabled && text.count + textToInsert.count <= digit && Validator.isOnlyNumeric(text: textToInsert) {
text.append(textToInsert)
sendActions(for: .editingChanged)
}
}
public func deleteBackward() {
if isEnabled && !text.isEmpty {
text.removeLast()
sendActions(for: .editingChanged)
}
}
// MARK: - UIResponder
@discardableResult
override public func becomeFirstResponder() -> Bool {
showCursor()
return super.becomeFirstResponder()
}
override public var canBecomeFirstResponder: Bool {
return true
}
@discardableResult
override public func resignFirstResponder() -> Bool {
hiddenCursor()
return super.resignFirstResponder()
}
// MARK: - private class
private class ContainerItemView<T: UIView & ItemType>: UIView {
var itemView: T
private let surfaceView: UIView = .init()
private var didTapHandler: (() -> ())?
init(itemView: T) {
self.itemView = itemView
super.init(frame: .zero)
addSubview(itemView)
addSubview(surfaceView)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didTap))
surfaceView.addGestureRecognizer(tapGesture)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
itemView.frame = bounds
surfaceView.frame = bounds
}
func setHandler(handler: @escaping () -> ()) {
didTapHandler = handler
}
@objc private func didTap() {
if let handler = didTapHandler {
handler()
}
}
}
}