// // TextField.swift // MVMCoreUI // // Created by Kevin Christiano on 11/18/19. // Copyright © 2019 Verizon Wireless. All rights reserved. // import UIKit public protocol TextFieldDidDeleteProtocol: class { func textFieldDidDelete() } @objcMembers open class TextField: UITextField { //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- private var initialSetupPerformed = false /// Set to true to hide the blinking textField cursor. public var hideBlinkingCaret = false //-------------------------------------------------- // MARK: - Delegate //-------------------------------------------------- /// Holds a reference to the delegating class so this class can internally influence the TextField behavior as well. public weak var didDeleteDelegate: TextFieldDidDeleteProtocol? //-------------------------------------------------- // MARK: - Initialization //-------------------------------------------------- public override init(frame: CGRect) { super.init(frame: .zero) initialSetup() } public convenience init() { self.init(frame: .zero) } public required init?(coder: NSCoder) { super.init(coder: coder) initialSetup() } public func initialSetup() { if !initialSetupPerformed { tintColor = .black initialSetupPerformed = true setupView() } } open override func caretRect(for position: UITextPosition) -> CGRect { if hideBlinkingCaret { return .zero } let caretRect = super.caretRect(for: position) return CGRect(origin: caretRect.origin, size: CGSize(width: 1, height: caretRect.height)) } open override func deleteBackward() { super.deleteBackward() didDeleteDelegate?.textFieldDidDelete() } } /// MARK:- MVMCoreViewProtocol extension TextField: MVMCoreViewProtocol { open func updateView(_ size: CGFloat) {} /// Will be called only once. open func setupView() { translatesAutoresizingMaskIntoConstraints = false insetsLayoutMarginsFromSafeArea = false } } /// MARK:- MoleculeViewProtocol extension TextField: MoleculeViewProtocol { open func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable : Any]?) { if let color = model.backgroundColor?.uiColor { backgroundColor = color } } open func reset() { backgroundColor = .clear } }