// // TextField.swift // VDS // // Created by Matt Bruce on 5/1/24. // import Foundation import UIKit import Combine @objc(VDSTextField) open class TextField: UITextField, ViewProtocol, Errorable { //-------------------------------------------------- // MARK: - Initializers //-------------------------------------------------- required public init() { super.init(frame: .zero) initialSetup() } public override init(frame: CGRect) { super.init(frame: .zero) initialSetup() } public required init?(coder: NSCoder) { super.init(coder: coder) initialSetup() } //-------------------------------------------------- // MARK: - Combine Properties //-------------------------------------------------- /// Set of Subscribers for any Publishers for this Control. open var subscribers = Set() //-------------------------------------------------- // MARK: - Private Properties //-------------------------------------------------- private var initialSetupPerformed = false private var horizontalPadding: CGFloat = 0 //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- /// Key of whether or not updateView() is called in setNeedsUpdate() open var shouldUpdateView: Bool = true open var surface: Surface = .light { didSet { setNeedsUpdate() } } open var showError: Bool = false { didSet { setNeedsUpdate() } } open var errorText: String? { didSet { setNeedsUpdate() } } //-------------------------------------------------- // MARK: - Lifecycle //-------------------------------------------------- open func initialSetup() { if !initialSetupPerformed { initialSetupPerformed = true backgroundColor = .clear translatesAutoresizingMaskIntoConstraints = false setup() setNeedsUpdate() } } open func setup() { let accessView = UIView(frame: .init(origin: .zero, size: .init(width: UIScreen.main.bounds.width, height: 44))) accessView.backgroundColor = .white accessView.addBorder(side: .top, width: 1, color: .lightGray) let done = UIButton(type: .system) done.setTitle("Done", for: .normal) done.translatesAutoresizingMaskIntoConstraints = false done.addTarget(self, action: #selector(doneButtonAction), for: .touchUpInside) accessView.addSubview(done) done.pinCenterY() .pinTrailing(16) inputAccessoryView = accessView } @objc func doneButtonAction() { // Resigns the first responder status when 'Done' is tapped resignFirstResponder() } open func updateView() {} open func updateAccessibility() { if let errorText, showError { accessibilityLabel = "error, \(errorText)" } else { accessibilityLabel = nil } } open func reset() { shouldUpdateView = false surface = .light text = nil shouldUpdateView = true setNeedsUpdate() } //-------------------------------------------------- // MARK: - Overrides //-------------------------------------------------- open override func textRect(forBounds bounds: CGRect) -> CGRect { let rect = super.textRect(forBounds: bounds) return rect.insetBy(dx: -horizontalPadding, dy: 0) } open override func editingRect(forBounds bounds: CGRect) -> CGRect { let rect = super.editingRect(forBounds: bounds) return rect.insetBy(dx: -horizontalPadding, dy: 0) } open override func placeholderRect(forBounds bounds: CGRect) -> CGRect { let rect = super.placeholderRect(forBounds: bounds) return rect.insetBy(dx: -horizontalPadding, dy: 0) } open override var isSecureTextEntry: Bool { didSet { if isFirstResponder { _ = becomeFirstResponder() } } } open override func becomeFirstResponder() -> Bool { let success = super.becomeFirstResponder() if isSecureTextEntry, let text { self.text?.removeAll() insertText(text) } return success } } extension UITextField { public func cursorPosition(range: NSRange, replacementString string: String, rawNumber: String, formattedNumber: String) -> UITextPosition? { let start = range.location let length = string.count let newCursorLocation = start + length // Adjust the cursor position to skip over formatting characters var formattedCharacterCount = 0 for (index, character) in formattedNumber.enumerated() { if index >= newCursorLocation + formattedCharacterCount { break } if !character.isNumber { formattedCharacterCount += 1 } } let finalCursorLocation = min(newCursorLocation + formattedCharacterCount, formattedNumber.count) return position(from: beginningOfDocument, offset: finalCursorLocation) } }