113 lines
3.2 KiB
Swift
113 lines
3.2 KiB
Swift
//
|
|
// TextField.swift
|
|
// MVMCoreUI
|
|
//
|
|
// Created by Kevin Christiano on 11/18/19.
|
|
// Copyright © 2019 Verizon Wireless. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
public protocol TextInputDidDeleteProtocol: AnyObject {
|
|
func textInputDidDelete()
|
|
}
|
|
|
|
|
|
@objcMembers open class TextField: UITextField, ViewMaskingProtocol {
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Properties
|
|
//--------------------------------------------------
|
|
|
|
private var initialSetupPerformed = false
|
|
|
|
/// Set to true to hide the blinking textField cursor.
|
|
public var hideBlinkingCaret = false
|
|
public var enableClipboardActions: Bool = true
|
|
public var shouldMaskWhileRecording: Bool = true
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Delegate
|
|
//--------------------------------------------------
|
|
|
|
/// Holds a reference to the delegating class so this class can internally influence the TextField behavior as well.
|
|
public weak var didDeleteDelegate: TextInputDidDeleteProtocol?
|
|
|
|
//--------------------------------------------------
|
|
// 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 = .mvmBlack
|
|
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?.textInputDidDelete()
|
|
}
|
|
|
|
open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool { enableClipboardActions }
|
|
}
|
|
|
|
/// 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 {
|
|
|
|
public func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
|
|
|
|
if let color = model.backgroundColor?.uiColor {
|
|
backgroundColor = color
|
|
}
|
|
|
|
if let accessibilityIdentifier = model.accessibilityIdentifier {
|
|
self.accessibilityIdentifier = accessibilityIdentifier
|
|
}
|
|
|
|
shouldMaskWhileRecording = model.shouldMaskRecordedView ?? true
|
|
}
|
|
|
|
open func reset() {
|
|
backgroundColor = .clear
|
|
}
|
|
}
|