// // 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 { //-------------------------------------------------- // 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 { didSet { textViewModel?.showsPlaceholder = isShowingPlaceholder } } /// Set to true to hide the blinking textField cursor. public var hideBlinkingCaret = false public var textViewModel: TextViewModel? { return model as? TextViewModel } //-------------------------------------------------- // MARK: - Drawing Properties //-------------------------------------------------- private(set) var fieldState: FieldState = .original { didSet (oldState) { // Will not update if new state is the same as old. if fieldState != oldState { DispatchQueue.main.async { [weak self] in guard let self = self else { return } self.fieldState.setStateUI(for: self) } } } } /// Determines if the top, left, and right borders should be drawn. private var hideBorders = false public var borderStrokeColor: UIColor = .mvmCoolGray3 public var bottomStrokeColor: UIColor = .mvmBlack private var borderPath: UIBezierPath = UIBezierPath() private var bottomPath: UIBezierPath = UIBezierPath() //-------------------------------------------------- // MARK: - Property Observers //-------------------------------------------------- private var _isEnabled: Bool = true private var _showError: Bool = false private var _isSelected: Bool = false public var isEnabled: Bool { get { return _isEnabled } set (enabled) { _isEnabled = enabled _isSelected = false _showError = false fieldState = enabled ? .original : .disabled } } public var showError: Bool { get { return _showError } set (error) { _showError = error _isEnabled = true _isSelected = false fieldState = error ? .error : .original } } public var isSelected: Bool { get { return _isSelected } set (selected) { _isSelected = selected _isEnabled = true if _showError { fieldState = selected ? .selectedError : .error } else { fieldState = selected ? .selected : .original } } } //-------------------------------------------------- // 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 font = textViewModel?.fontStyle.getFont() isEditable = true isOpaque = false } open func reset() { text = "" inputAccessoryView?.removeFromSuperview() initialConfiguration() } open override func layoutSubviews() { super.layoutSubviews() setNeedsDisplay() } //-------------------------------------------------- // MARK: - Draw //-------------------------------------------------- /// This handles the top, left, and right border lines. open override func draw(_ rect: CGRect) { super.draw(rect) borderPath.removeAllPoints() bottomPath.removeAllPoints() if !hideBorders { // Brings the other half of the line inside the view to prevent line cropping. let origin = bounds.origin let size = frame.size let insetLean: CGFloat = 0.5 borderPath.lineWidth = 1 // Drawing begins and ends from the bottom left. borderPath.move(to: CGPoint(x: origin.x + insetLean, y: origin.y + size.height)) borderPath.addLine(to: CGPoint(x: origin.x + insetLean, y: origin.y + insetLean)) borderPath.addLine(to: CGPoint(x: origin.x + size.width - insetLean, y: origin.y + insetLean)) borderPath.addLine(to: CGPoint(x: origin.x + size.width - insetLean, y: origin.y + size.height)) borderStrokeColor.setStroke() borderPath.stroke() let lineWidth: CGFloat = showError || isSelected ? 4 : 1 bottomPath.lineWidth = lineWidth bottomPath.move(to: CGPoint(x: origin.x + size.width, y: origin.y + size.height - (lineWidth / 2))) bottomPath.addLine(to: CGPoint(x: origin.x, y: origin.y + size.height - (lineWidth / 2))) bottomStrokeColor.setStroke() bottomPath.stroke() } } //-------------------------------------------------- // MARK: - Draw States //-------------------------------------------------- public enum FieldState { case original case error case selectedError case selected case disabled public func setStateUI(for inputField: TextView) { switch self { case .original: inputField.originalUI() case .error: inputField.errorUI() case .selectedError: inputField.selectedErrorUI() case .selected: inputField.selectedUI() case .disabled: inputField.disabledUI() } inputField.setNeedsDisplay() } } open func originalUI() { isEditable = textViewModel?.editable ?? true isUserInteractionEnabled = true hideBorders = textViewModel?.hideBorders ?? false borderStrokeColor = .mvmCoolGray3 bottomStrokeColor = .mvmBlack textColor = isShowingPlaceholder ? textViewModel?.placeholderTextColor.uiColor : textViewModel?.enabledTextColor.uiColor } open func errorUI() { isEditable = textViewModel?.editable ?? true isUserInteractionEnabled = true hideBorders = textViewModel?.hideBorders ?? false borderStrokeColor = .mvmOrange bottomStrokeColor = .mvmOrange textColor = textViewModel?.enabledTextColor.uiColor } open func selectedErrorUI() { isEditable = textViewModel?.editable ?? true isUserInteractionEnabled = true hideBorders = textViewModel?.hideBorders ?? false borderStrokeColor = .mvmBlack bottomStrokeColor = .mvmOrange textColor = textViewModel?.enabledTextColor.uiColor } open func selectedUI() { isEditable = textViewModel?.editable ?? true isUserInteractionEnabled = true hideBorders = textViewModel?.hideBorders ?? false borderStrokeColor = .mvmBlack bottomStrokeColor = .mvmBlack textColor = textViewModel?.enabledTextColor.uiColor } open func disabledUI() { isEditable = textViewModel?.editable ?? false isUserInteractionEnabled = false hideBorders = textViewModel?.hideBorders ?? false borderStrokeColor = .mvmCoolGray3 bottomStrokeColor = .mvmCoolGray3 textColor = textViewModel?.disabledTextColor.uiColor } //-------------------------------------------------- // 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 let placeholder = textViewModel?.placeholder, !placeholder.isEmpty && text.isEmpty { setPlaceholderContentTraits() } } public func setTextContentTraits() { isShowingPlaceholder = false text = "" font = textViewModel?.fontStyle.getFont() textColor = textViewModel?.enabledTextColor.uiColor } public func setPlaceholderContentTraits() { isShowingPlaceholder = true textColor = textViewModel?.placeholderTextColor.uiColor font = textViewModel?.placeholderFontStyle.getFont() text = textViewModel?.placeholder } //-------------------------------------------------- // MARK: - UITextViewDelegate //-------------------------------------------------- @objc public func textViewShouldBeginEditing(_ textView: UITextView) -> Bool { return proprietorTextDelegate?.textViewShouldBeginEditing?(textView) ?? true } @objc public func textViewDidBeginEditing(_ textView: UITextView) { setTextAppearance() isSelected = true 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) { textViewModel?.text = textView.text proprietorTextDelegate?.textViewDidChange?(textView) } @objc public func textViewShouldEndEditing(_ textView: UITextView) -> Bool { return proprietorTextDelegate?.textViewShouldEndEditing?(textView) ?? true } @objc public func textViewDidEndEditing(_ textView: UITextView) { setPlaceholderIfAvailable() isSelected = false proprietorTextDelegate?.textViewDidEndEditing?(textView) } } // MARK:- MoleculeViewProtocol extension TextView: MoleculeViewProtocol { open func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) { self.model = model self.delegateObject = delegateObject if let color = model.backgroundColor?.uiColor { backgroundColor = color } guard let model = model as? TextViewModel else { return } heightConstraint?.isActive = false if let height = model.height { heightConstraint = heightAnchor.constraint(equalToConstant: height) heightConstraint?.isActive = true } isEditable = model.editable textAlignment = model.textAlignment textColor = model.enabledTextColor.uiColor hideBorders = model.hideBorders text = model.text uiTextViewDelegate = delegateObject?.uiTextViewDelegate if let accessibilityText = model.accessibilityText { accessibilityLabel = accessibilityText } switch model.type { case .secure, .password: isSecureTextEntry = true case .number: keyboardType = .numberPad case .email: keyboardType = .emailAddress default: break } font = model.fontStyle.getFont() setPlaceholderIfAvailable() if isEditable { FormValidator.setupValidation(for: model, delegate: delegateObject?.formHolderDelegate) MVMCoreUICommonViewsUtility.addDismissToolbar(to: self, delegate: delegateObject?.uiTextViewDelegate) if (model.selected ?? false) && !model.wasInitiallySelected { model.wasInitiallySelected = true DispatchQueue.main.async { self.becomeFirstResponder() } } } if !model.enabled { isEnabled = false } } }