109 lines
2.9 KiB
Swift
109 lines
2.9 KiB
Swift
//
|
|
// 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
|
|
//--------------------------------------------------
|
|
|
|
open var json: [AnyHashable: Any]?
|
|
|
|
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 {
|
|
initialSetupPerformed = true
|
|
setupView()
|
|
}
|
|
}
|
|
|
|
open override func caretRect(for position: UITextPosition) -> CGRect {
|
|
|
|
if hideBlinkingCaret {
|
|
return .zero
|
|
}
|
|
|
|
return super.caretRect(for: position)
|
|
}
|
|
|
|
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:- MVMCoreUIMoleculeViewProtocol
|
|
extension TextField: MVMCoreUIMoleculeViewProtocol {
|
|
|
|
open func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
|
|
self.json = json
|
|
|
|
guard let dictionary = json else { return }
|
|
|
|
if let backgroundColorString = dictionary.optionalStringForKey(KeyBackgroundColor) {
|
|
backgroundColor = UIColor.mfGet(forHex: backgroundColorString)
|
|
}
|
|
|
|
if let text = dictionary[KeyText] as? String {
|
|
self.text = text
|
|
}
|
|
}
|
|
|
|
open func reset() {
|
|
backgroundColor = .clear
|
|
}
|
|
}
|