237 lines
6.9 KiB
Swift
237 lines
6.9 KiB
Swift
//
|
|
// TextView.swift
|
|
// MVMCoreUI
|
|
//
|
|
// Created by Kevin Christiano on 4/1/20.
|
|
// Copyright © 2020 Verizon Wireless. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
|
|
@objc open class TextView: UITextView, UITextViewDelegate {
|
|
//--------------------------------------------------
|
|
// MARK: - Properties
|
|
//--------------------------------------------------
|
|
|
|
open var model: MoleculeModelProtocol?
|
|
|
|
private var initialSetupPerformed = false
|
|
|
|
/// Set to true to hide the blinking textField cursor.
|
|
public var hideBlinkingCaret = false
|
|
|
|
weak var bottomLine: SeparatorView?
|
|
|
|
public var placeholderFont: UIFont = MFStyler.fontRegularMicro() {
|
|
didSet {
|
|
if text.isEmpty {
|
|
font = placeholderFont
|
|
}
|
|
}
|
|
}
|
|
|
|
public var placeholder: String = "" {
|
|
didSet {
|
|
if text.isEmpty {
|
|
text = placeholder
|
|
}
|
|
}
|
|
}
|
|
|
|
public var placeholderTextColor: UIColor = .mvmCoolGray3
|
|
|
|
public var displaysPlaceholder = false
|
|
|
|
private(set) var textStore: String = ""
|
|
|
|
public override var text: String! {
|
|
didSet {
|
|
if displaysPlaceholder && text.isEmpty {
|
|
text = placeholder
|
|
textColor = placeholderTextColor
|
|
} else {
|
|
textColor = .mvmBlack
|
|
}
|
|
}
|
|
}
|
|
|
|
private(set) var textFont: UIFont = MFStyler.fontB2()!
|
|
|
|
public override var font: UIFont? {
|
|
didSet {
|
|
if displaysPlaceholder && text.isEmpty {
|
|
font = placeholderFont
|
|
} else {
|
|
textColor = .mvmBlack
|
|
}
|
|
}
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// 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?
|
|
|
|
/// If you're using a ViewController, you must set this to it
|
|
public weak var uiTextViewDelegate: UITextViewDelegate? {
|
|
get { return delegate }
|
|
set { delegate = newValue }
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Constraint
|
|
//--------------------------------------------------
|
|
|
|
public var heightConstraint: NSLayoutConstraint?
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Initialization
|
|
//--------------------------------------------------
|
|
|
|
public override init(frame: CGRect, textContainer: NSTextContainer?) {
|
|
super.init(frame: .zero, textContainer: nil)
|
|
initialSetup()
|
|
}
|
|
|
|
public convenience init() {
|
|
self.init(frame: .zero, textContainer: nil)
|
|
}
|
|
|
|
public required init?(coder: NSCoder) {
|
|
super.init(coder: coder)
|
|
initialSetup()
|
|
}
|
|
|
|
convenience init(placeholderText: String, delegate: UITextViewDelegate?) {
|
|
self.init(frame: .zero, textContainer: nil)
|
|
self.delegate = delegate
|
|
displaysPlaceholder = true
|
|
self.placeholder = placeholderText
|
|
}
|
|
|
|
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?.textFieldDidDelete()
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Observing Methods
|
|
//--------------------------------------------------
|
|
|
|
/// Executes on UITextView.textDidEndEditingNotification
|
|
@objc open func dismissFieldInput() {
|
|
|
|
resignFirstResponder()
|
|
}
|
|
|
|
//#pragma Mark UITextView Delegate methods forwarding
|
|
@objc public func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
|
|
|
|
return delegate?.textViewShouldBeginEditing?(textView) ?? true
|
|
}
|
|
|
|
@objc public func textViewDidBeginEditing(_ textView: UITextView) {
|
|
|
|
delegate?.textViewDidBeginEditing?(textView)
|
|
}
|
|
|
|
@objc public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
|
|
|
|
return delegate?.textView?(textView, shouldChangeTextIn: range, replacementText: text) ?? true
|
|
}
|
|
|
|
@objc public func textViewDidChange(_ textView: UITextView) {
|
|
|
|
delegate?.textViewDidChange?(textView)
|
|
}
|
|
|
|
@objc public func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
|
|
|
|
return delegate?.textViewShouldEndEditing?(textView) ?? true
|
|
}
|
|
|
|
@objc public func textViewDidEndEditing(_ textView: UITextView) {
|
|
|
|
delegate?.textViewDidEndEditing?(textView)
|
|
}
|
|
}
|
|
|
|
/// MARK:- MVMCoreViewProtocol
|
|
extension TextView: MVMCoreViewProtocol {
|
|
|
|
open func updateView(_ size: CGFloat) { }
|
|
|
|
/// Will be called only once.
|
|
open func setupView() {
|
|
|
|
translatesAutoresizingMaskIntoConstraints = false
|
|
insetsLayoutMarginsFromSafeArea = false
|
|
showsVerticalScrollIndicator = false
|
|
showsHorizontalScrollIndicator = false
|
|
clipsToBounds = true
|
|
smartQuotesType = .no
|
|
smartDashesType = .no
|
|
smartInsertDeleteType = .no
|
|
font = textFont
|
|
layer.borderWidth = 1
|
|
layer.borderColor = UIColor.mvmBlack.cgColor
|
|
isEditable = true
|
|
}
|
|
}
|
|
|
|
/// MARK:- MoleculeViewProtocol
|
|
extension TextView: MoleculeViewProtocol {
|
|
|
|
open func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
|
|
self.model = model
|
|
|
|
if let color = model.backgroundColor?.uiColor {
|
|
backgroundColor = color
|
|
}
|
|
|
|
guard let model = model as? TextViewModel else { return }
|
|
|
|
if let height = model.height {
|
|
heightConstraint = heightAnchor.constraint(equalToConstant: height)
|
|
heightConstraint?.isActive = true
|
|
}
|
|
|
|
isEditable = model.isEditable
|
|
textAlignment = model.textAlignment
|
|
textColor = model.textColor.uiColor
|
|
text = model.text
|
|
placeholder = model.placeholder
|
|
uiTextViewDelegate = delegateObject?.uiTextViewDelegate
|
|
MVMCoreUICommonViewsUtility.addDismissToolbar(to: self, delegate: delegateObject?.uiTextViewDelegate)
|
|
}
|
|
|
|
open func reset() {
|
|
|
|
setNeedsDisplay()
|
|
backgroundColor = .clear
|
|
text = ""
|
|
}
|
|
}
|