OTPView.swift
2.89 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
//
// OTPView.swift
// InterQR-Internship
//
// Created by Дмитрий Тимофеев on 02.07.2022.
//
import Foundation
import UIKit
protocol OTPViewDelegate: AnyObject {
func didFullCodeCompletion(_ otpView: OTPView)
}
class OTPView: UIView {
weak var delegate: OTPViewDelegate?
var digitViews: [UILabel] = []
var code: String? {
return textField.text
}
var isError: Bool = false {
didSet {
handleErrorUI(isError)
}
}
let textField: UITextField = {
let obj = UITextField()
obj.isHidden = true
obj.keyboardType = .numberPad
return obj
}()
private lazy var stackView: UIStackView = {
let obj = UIStackView(arrangedSubviews: digitViews)
obj.axis = .horizontal
obj.spacing = 11
obj.distribution = .fillEqually
obj.semanticContentAttribute = .forceLeftToRight
return obj
}()
override init(frame: CGRect) {
super.init(frame: frame)
initDigits()
setup()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setup()
}
private func initDigits() {
for i in 0...3 {
digitViews.append(initLabel())
}
}
private func initLabel() -> UILabel {
let obj = UILabel()
obj.font = .skModernist(type: .bold, ofSize: 16)
obj.textColor = .black
obj.textAlignment = .center
obj.backgroundColor = .TextFieldColor.general
obj.layer.cornerRadius = 15
obj.clipsToBounds = true
return obj
}
private func setup() {
addSubview(textField)
addSubview(stackView)
stackView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
textField.addTarget(self, action: #selector(didTextFieldChanged(_:)), for: .editingChanged)
}
override func addSubview(_ view: UIView) {
super.addSubview(view)
textField.becomeFirstResponder()
}
@objc func didTextFieldChanged(_ sender: UITextField) {
guard var code = sender.text else {
return
}
if code.count > 4 {
code.removeLast()
} else {
if code.count == 4 {
delegate?.didFullCodeCompletion(self)
textField.resignFirstResponder()
}
}
digitViews.enumerated().forEach { index, item in
item.text = (code.count-1 >= index ? String(code[code.index(code.startIndex, offsetBy: index)]) : "")
}
}
private func handleErrorUI(_ isError: Bool) {
colorСhanger()
}
private func colorСhanger() {
digitViews.self.forEach { label in
label.backgroundColor = .TextFieldColor.error
label.textColor = .TextColor.redError
}
}
}