// // TextView.swift // VDS // // Created by Matt Bruce on 2/29/24. // import Foundation import UIKit import Combine import VDSCoreTokens @objc(VDSTextView) open class TextView: UITextView, ViewProtocol, Errorable { //-------------------------------------------------- // MARK: - Initializers //-------------------------------------------------- required public init() { super.init(frame: .zero, textContainer: nil) initialSetup() } public override init(frame: CGRect, textContainer: NSTextContainer?) { super.init(frame: frame, textContainer: textContainer) initialSetup() } public required init?(coder: NSCoder) { super.init(coder: coder) initialSetup() } //-------------------------------------------------- // MARK: - Combine Properties //-------------------------------------------------- /// Set of Subscribers for any Publishers for this Control. open var subscribers = Set() //-------------------------------------------------- // MARK: - Private Properties //-------------------------------------------------- private var initialSetupPerformed = false //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- open var placeholder: String? { didSet { placeholderLabel.text = placeholder } } open var placeholderLabel = Label().with { $0.textColorConfiguration = ViewColorConfiguration().with { $0.setSurfaceColors(VDSColor.interactiveDisabledOnlight, VDSColor.interactiveDisabledOndark, forDisabled: true) $0.setSurfaceColors(VDSColor.elementsSecondaryOnlight, VDSColor.elementsSecondaryOndark, forDisabled: false) }.eraseToAnyColorable() } /// Key of whether or not updateView() is called in setNeedsUpdate() open var shouldUpdateView: Bool = true open var surface: Surface = .light { didSet { setNeedsUpdate() } } /// Array of LabelAttributeModel objects used in rendering the text. open var textAttributes: [any LabelAttributeModel]? { didSet { setNeedsUpdate() } } /// TextStyle used on the titleLabel. open var textStyle: TextStyle { .defaultStyle } /// Will determine if a scaled font should be used for the titleLabel font. open var useScaledFont: Bool = false { didSet { setNeedsUpdate() } } open var isEnabled: Bool = true { didSet { setNeedsUpdate() } } open var textColorConfiguration: AnyColorable = ViewColorConfiguration().with { $0.setSurfaceColors(VDSColor.interactiveDisabledOnlight, VDSColor.interactiveDisabledOndark, forDisabled: true) $0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forDisabled: false) }.eraseToAnyColorable(){ didSet { setNeedsUpdate() }} open var showError: Bool = false { didSet { setNeedsUpdate() } } open var errorText: String? { didSet { setNeedsUpdate() } } open override var textColor: UIColor? { get { textColorConfiguration.getColor(self) } set { } } override public var text: String! { get { super.text } set { super.text = newValue updateLabel() } } override public var textAlignment: NSTextAlignment { didSet { if textAlignment != oldValue { // Text alignment can be part of our paragraph style, so we may need to // re-style when changed placeholderLabel.textAlignment = textAlignment updateLabel() } } } //-------------------------------------------------- // MARK: - Lifecycle //-------------------------------------------------- open func initialSetup() { if !initialSetupPerformed { initialSetupPerformed = true backgroundColor = .clear translatesAutoresizingMaskIntoConstraints = false setup() setNeedsUpdate() } } open func setup() { let accessView = UIView(frame: .init(origin: .zero, size: .init(width: UIScreen.main.bounds.width, height: 44))) accessView.backgroundColor = .white accessView.addBorder(side: .top, width: 1, color: .lightGray) let done = UIButton(type: .system) done.setTitle("Done", for: .normal) done.translatesAutoresizingMaskIntoConstraints = false done.addTarget(self, action: #selector(doneButtonAction), for: .touchUpInside) accessView.addSubview(done) done.pinCenterY() .pinTrailing(16) inputAccessoryView = accessView addSubview(placeholderLabel) placeholderLabel.pinToSuperView() } @objc func doneButtonAction() { // Resigns the first responder status when 'Done' is tapped resignFirstResponder() } open func updateView() { updateLabel() } open func updateAccessibility() { if let errorText, showError { accessibilityLabel = "error, \(errorText)" } else { accessibilityLabel = nil } } open func reset() { shouldUpdateView = false surface = .light text = nil shouldUpdateView = true setNeedsUpdate() } open override func layoutSubviews() { super.layoutSubviews() placeholderLabel.preferredMaxLayoutWidth = textContainer.size.width - textContainer.lineFragmentPadding * 2 } //-------------------------------------------------- // MARK: - Accessibility //-------------------------------------------------- open var accessibilityAction: ((TextView) -> Void)? open override var isAccessibilityElement: Bool { get { var block: AXBoolReturnBlock? // if #available(iOS 17, *) { // block = isAccessibilityElementBlock // } if block == nil { block = bridge_isAccessibilityElementBlock } if let block { return block() } else { return super.isAccessibilityElement } } set { super.isAccessibilityElement = newValue } } open override var accessibilityLabel: String? { get { var block: AXStringReturnBlock? // if #available(iOS 17, *) { // block = accessibilityLabelBlock // } if block == nil { block = bridge_accessibilityLabelBlock } if let block { return block() } else { return super.accessibilityLabel } } set { super.accessibilityLabel = newValue } } open override var accessibilityHint: String? { get { var block: AXStringReturnBlock? // if #available(iOS 17, *) { // block = accessibilityHintBlock // } if block == nil { block = bridge_accessibilityHintBlock } if let block { return block() } else { return super.accessibilityHint } } set { super.accessibilityHint = newValue } } open override var accessibilityValue: String? { get { var block: AXStringReturnBlock? // if #available(iOS 17, *) { // block = accessibilityHintBlock // } if block == nil { block = bridge_accessibilityValueBlock } if let block{ return block() } else { return super.accessibilityValue } } set { super.accessibilityValue = newValue } } open override func accessibilityActivate() -> Bool { guard isEnabled, isUserInteractionEnabled else { return false } // if #available(iOS 17, *) { // if let block = accessibilityAction { // block(self) // return true // } else if let block = accessibilityActivateBlock { // return block() // // } else if let block = bridge_accessibilityActivateBlock { // return block() // // } else { // return super.accessibilityActivate() // // } // // } else { if let block = accessibilityAction { block(self) return true } else if let block = bridge_accessibilityActivateBlock { return block() } else { return super.accessibilityActivate() } // } } //-------------------------------------------------- // MARK: - Private Methods //-------------------------------------------------- private func updateLabel() { //clear the arrays holding actions accessibilityCustomActions = [] if let text, !text.isEmpty { //create the primary string let mutableText = NSMutableAttributedString.mutableText(for: text, textStyle: textStyle, useScaledFont: useScaledFont, textColor: textColor!, alignment: textAlignment, lineBreakMode: .byWordWrapping) //apply any attributes if let attributes = textAttributes { mutableText.apply(attributes: attributes) } attributedText = mutableText } else { attributedText = nil } placeholderLabel.textStyle = textStyle placeholderLabel.isHidden = !text.isEmpty placeholderLabel.surface = surface } }