-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathPasscodeKitText.swift
More file actions
executable file
·76 lines (61 loc) · 2.31 KB
/
PasscodeKitText.swift
File metadata and controls
executable file
·76 lines (61 loc) · 2.31 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
//
// Copyright (c) 2023 Related Code - https://relatedcode.com
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import UIKit
// MARK: - PasscodeKitText
class PasscodeKitText: UITextField {
private let radius: CGFloat = 8
private let spacing: CGFloat = 20
override init(frame: CGRect) {
super.init(frame: frame)
tintColor = .clear
textColor = .clear
borderStyle = .none
keyboardType = .numberPad
textContentType = .password
font = UIFont.systemFont(ofSize: 0)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return false
}
override func layoutSubviews() {
super.layoutSubviews()
layer.sublayers?.forEach { layer in
if (layer.name == "PasscodeKitSublayer") {
layer.removeFromSuperlayer()
}
}
let currentLength = text?.count ?? 0
let passcodeLength = PasscodeKit.passcodeLength
let circles = CGFloat(passcodeLength)
let layerWidth = (2 * radius * circles) + spacing * (circles - 1)
let layerXPos = (frame.size.width - layerWidth) / 2
let layerYPos = (frame.size.height - 2 * radius) / 2
let xlayer = CALayer()
xlayer.frame = CGRect(x: layerXPos, y: layerYPos, width: layerWidth, height: 2 * radius)
xlayer.name = "PasscodeKitSublayer"
layer.addSublayer(xlayer)
let circleCenter = CGPoint(x: radius, y: radius)
let circlePath = UIBezierPath(arcCenter: circleCenter, radius: radius, startAngle: 0, endAngle: 2 * .pi, clockwise: false)
let circleColor = PasscodeKit.textColor.cgColor
for i in 0..<passcodeLength {
let circle = CAShapeLayer()
circle.frame = CGRect(x: (2 * radius + spacing) * CGFloat(i), y: 0, width: 2 * radius, height: 2 * radius)
circle.path = circlePath.cgPath
circle.fillColor = (i < currentLength) ? circleColor : nil
circle.strokeColor = circleColor
circle.lineWidth = 1
xlayer.addSublayer(circle)
}
}
}