mvm_core_ui/MVMCoreUI/BaseClasses/TextView.swift
2020-05-08 11:50:36 -04:00

241 lines
7.4 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, MVMCoreViewProtocol, MoleculeViewProtocol {
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
open var model: MoleculeModelProtocol?
private var initialSetupPerformed = false
/// If true then text textView is currently displaying the stored placeholder text as there is not content to display.
public var isShowingPlaceholder: Bool = false
/// Set to true to hide the blinking textField cursor.
public var hideBlinkingCaret = false
public var placeholder = ""
public var fontStyle: Styler.Font = Styler.Font.RegularBodySmall
public var placeholderFontStyle: Styler.Font = Styler.Font.RegularMicro
public var placeholderTextColor: UIColor = .mvmCoolGray3
public var isEnabled: Bool = true {
didSet {
isUserInteractionEnabled = isEnabled
}
}
//--------------------------------------------------
// 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?
/// Holds a reference to the delegating class so this class can internally influence the TextField behavior as well.
private weak var proprietorTextDelegate: UITextViewDelegate?
/// If you're using a ViewController, you must set this to it.
public weak var uiTextViewDelegate: UITextViewDelegate? {
get { return delegate }
set {
delegate = self
proprietorTextDelegate = newValue
}
}
var delegateObject: MVMCoreUIDelegateObject?
//--------------------------------------------------
// 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(delegate: UITextViewDelegate) {
self.init(frame: .zero, textContainer: nil)
self.delegate = delegate
}
//--------------------------------------------------
// MARK: - Lifecycle
//--------------------------------------------------
public func initialSetup() {
if !initialSetupPerformed {
tintColor = .mvmBlack
initialSetupPerformed = true
setupView()
}
}
open func updateView(_ size: CGFloat) {
setNeedsDisplay()
}
/// Will be called only once.
open func setupView() {
translatesAutoresizingMaskIntoConstraints = false
initialConfiguration()
}
public func initialConfiguration() {
insetsLayoutMarginsFromSafeArea = false
showsVerticalScrollIndicator = false
showsHorizontalScrollIndicator = false
isSecureTextEntry = false
// textContainerInset = UIEdgeInsets(top: Padding.Three, left: Padding.Three, bottom: Padding.Three, right: Padding.Three)
backgroundColor = .mvmWhite
clipsToBounds = true
smartQuotesType = .no
smartDashesType = .no
smartInsertDeleteType = .no
inputAccessoryView = nil
isAccessibilityElement = true
accessibilityTraits = .staticText
font = Styler.Font.RegularBodyLarge.getFont()
isEditable = true
isOpaque = false
}
open func reset() {
text = ""
textAlignment = .left
placeholder = ""
fontStyle = Styler.Font.RegularBodyLarge
placeholderFontStyle = Styler.Font.RegularMicro
placeholderTextColor = .mvmCoolGray3
font = Styler.Font.RegularBodyLarge.getFont()
keyboardType = .default
isEditable = true
isEnabled = true
inputAccessoryView?.removeFromSuperview()
inputAccessoryView = nil
initialConfiguration()
}
//--------------------------------------------------
// MARK: - Methods
//--------------------------------------------------
/// Alters the blinking caret line as per design standards.
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()
}
public func setTextAppearance() {
if isShowingPlaceholder {
setTextContentTraits()
}
}
public func setPlaceholderIfAvailable() {
if !placeholder.isEmpty && text.isEmpty {
setPlaceholderContentTraits()
}
}
public func setTextContentTraits() {
isShowingPlaceholder = false
text = ""
font = fontStyle.getFont()
textColor = .mvmBlack
}
public func setPlaceholderContentTraits() {
isShowingPlaceholder = true
textColor = placeholderTextColor
font = placeholderFontStyle.getFont()
text = placeholder
}
@objc func dismissFieldInput(_ sender: TextView) {
resignFirstResponder()
}
//--------------------------------------------------
// MARK: - UITextViewDelegate
//--------------------------------------------------
@objc public func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
return proprietorTextDelegate?.textViewShouldBeginEditing?(textView) ?? true
}
@objc public func textViewDidBeginEditing(_ textView: UITextView) {
setTextAppearance()
proprietorTextDelegate?.textViewDidBeginEditing?(textView)
}
@objc public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
return proprietorTextDelegate?.textView?(textView, shouldChangeTextIn: range, replacementText: text) ?? true
}
@objc public func textViewDidChange(_ textView: UITextView) {
proprietorTextDelegate?.textViewDidChange?(textView)
}
@objc public func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
return proprietorTextDelegate?.textViewShouldEndEditing?(textView) ?? true
}
@objc public func textViewDidEndEditing(_ textView: UITextView) {
setPlaceholderIfAvailable()
proprietorTextDelegate?.textViewDidEndEditing?(textView)
}
}