|
| 1 | +import UIKit |
| 2 | + |
| 3 | +public class OneTimePasscodeField: UIControl { |
| 4 | + private let contentStack = UIStackView() |
| 5 | + |
| 6 | + private var textFields: [OneTimePasscodeTextField] { |
| 7 | + // swiftlint:disable:next force_cast |
| 8 | + contentStack.arrangedSubviews as! [OneTimePasscodeTextField] |
| 9 | + } |
| 10 | + |
| 11 | + // swiftlint:disable:next weak_delegate |
| 12 | + private lazy var textFieldDelegate = OneTimePasscodeTextFieldDelegate(parentField: self) |
| 13 | + |
| 14 | + public var text: String { |
| 15 | + get { textFields.compactMap(\.text).joined() } |
| 16 | + set { autoFillTextField(with: newValue) } |
| 17 | + } |
| 18 | + |
| 19 | + @objc public dynamic var textColor = UIColor.black { |
| 20 | + didSet { |
| 21 | + textFields.forEach { |
| 22 | + $0.textColor = textColor |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + @objc public dynamic var textBackgroundColor = UIColor(white: 1, alpha: 0.5) { |
| 28 | + didSet { |
| 29 | + textFields.forEach { |
| 30 | + $0.backgroundColor = textBackgroundColor |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + @objc public dynamic var font = UIFont.preferredFont(forTextStyle: .largeTitle) { |
| 36 | + didSet { |
| 37 | + textFields.forEach { |
| 38 | + $0.font = font |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + override public init(frame: CGRect) { |
| 44 | + super.init(frame: frame) |
| 45 | + commonInit() |
| 46 | + } |
| 47 | + |
| 48 | + public required init?(coder: NSCoder) { |
| 49 | + super.init(coder: coder) |
| 50 | + commonInit() |
| 51 | + } |
| 52 | + |
| 53 | + private func commonInit() { |
| 54 | + // Handle touches inside this view, which without the following wouldn't trigger text entry |
| 55 | + addTarget(self, action: #selector(touchUpInside), for: .touchUpInside) |
| 56 | + |
| 57 | + contentStack.translatesAutoresizingMaskIntoConstraints = false |
| 58 | + contentStack.isUserInteractionEnabled = false |
| 59 | + contentStack.contentMode = .center |
| 60 | + contentStack.distribution = .fillEqually |
| 61 | + contentStack.alignment = .fill |
| 62 | + contentStack.spacing = 10 |
| 63 | + addSubview(contentStack) |
| 64 | + |
| 65 | + NSLayoutConstraint.activate([ |
| 66 | + contentStack.topAnchor.constraint(equalTo: topAnchor), |
| 67 | + contentStack.trailingAnchor.constraint(equalTo: trailingAnchor), |
| 68 | + contentStack.bottomAnchor.constraint(equalTo: bottomAnchor), |
| 69 | + contentStack.leadingAnchor.constraint(equalTo: leadingAnchor), |
| 70 | + ]) |
| 71 | + |
| 72 | + // Set up the text fields |
| 73 | + var previousTextField: OneTimePasscodeTextField? |
| 74 | + for _ in 0 ..< 6 { |
| 75 | + let textField = OneTimePasscodeTextField() |
| 76 | + textField.delegate = textFieldDelegate |
| 77 | + textField.backgroundColor = textBackgroundColor |
| 78 | + textField.font = font |
| 79 | + contentStack.addArrangedSubview(textField) |
| 80 | + |
| 81 | + // Create the linked list of fields |
| 82 | + previousTextField?.nextTextField = textField |
| 83 | + textField.previousTextField = previousTextField |
| 84 | + previousTextField = textField |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @objc private func touchUpInside() { |
| 89 | + _ = becomeFirstResponder() |
| 90 | + } |
| 91 | + |
| 92 | + override public func becomeFirstResponder() -> Bool { |
| 93 | + // Make sure to select the first empty text field, don't let the user start typing in the middle |
| 94 | + |
| 95 | + // swiftlint:disable:next force_unwrapping |
| 96 | + guard let emptyOrLastField = textFields.first(where: { $0.text!.isEmpty }) ?? textFields.last else { |
| 97 | + return super.becomeFirstResponder() |
| 98 | + } |
| 99 | + return emptyOrLastField.becomeFirstResponder() |
| 100 | + } |
| 101 | + |
| 102 | + override public func resignFirstResponder() -> Bool { |
| 103 | + textFields.first(where: \.isFirstResponder)?.resignFirstResponder() ?? true |
| 104 | + } |
| 105 | + |
| 106 | + @discardableResult |
| 107 | + func autoFillTextField(with string: String) -> Bool { |
| 108 | + // Only allow numbers to be entered |
| 109 | + guard CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: string)) else { |
| 110 | + return false |
| 111 | + } |
| 112 | + |
| 113 | + // Distribute the characters into each of the textfields |
| 114 | + var reversedCharacters = string.reversed().compactMap { String($0) } |
| 115 | + for textField in textFields { |
| 116 | + guard let char = reversedCharacters.popLast() else { break } |
| 117 | + textField.text = String(char) |
| 118 | + } |
| 119 | + |
| 120 | + return true |
| 121 | + } |
| 122 | + |
| 123 | + public func clearTextFields() { |
| 124 | + for textField in textFields { |
| 125 | + textField.text = "" |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments