// // 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? 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 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() } }