From 10fdd6bcb825fb4ffe4c641708e77e5bd2cc61ce Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Wed, 13 Mar 2024 09:09:45 -0500 Subject: [PATCH] updated TextView to mimic label/buttonbase Signed-off-by: Matt Bruce --- .../TextFields/TextArea/TextView.swift | 107 +++++++++++++----- 1 file changed, 81 insertions(+), 26 deletions(-) diff --git a/VDS/Components/TextFields/TextArea/TextView.swift b/VDS/Components/TextFields/TextArea/TextView.swift index ffb355d1..4df75455 100644 --- a/VDS/Components/TextFields/TextArea/TextView.swift +++ b/VDS/Components/TextFields/TextArea/TextView.swift @@ -70,21 +70,43 @@ open class TextView: UITextView, ViewProtocol { get { textColorConfiguration.getColor(self) } set { } } - - override public var text: String! { + + private enum TextSetMode { + case text + case attributedText + } + + private var textSetMode: TextSetMode = .text + + /// :nodoc: + open override var text: String! { get { super.text } set { - super.text = newValue - updateLabel() + // When text is set, we may need to re-style it as attributedText + // with the correct paragraph style to achieve the desired line height. + textSetMode = .text + styleText(newValue) } } - override public var textAlignment: NSTextAlignment { + /// :nodoc: + open override var attributedText: NSAttributedString? { + get { super.attributedText } + set { + // When text is set, we may need to re-style it as attributedText + // with the correct paragraph style to achieve the desired line height. + textSetMode = .attributedText + styleAttributedText(newValue) + } + } + + /// :nodoc: + open override 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 - updateLabel() + restyleText() } } } @@ -109,7 +131,7 @@ open class TextView: UITextView, ViewProtocol { } open func updateView() { - updateLabel() + restyleText() } open func updateAccessibility() {} @@ -126,26 +148,59 @@ open class TextView: UITextView, ViewProtocol { //-------------------------------------------------- // 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 + private func restyleText() { + if textSetMode == .text { + styleText(text) } else { - attributedText = nil + styleAttributedText(attributedText) + } + } + + private func styleText(_ newValue: String!) { + defer { invalidateIntrinsicContentSize() } + guard let newValue else { + // We don't need to use attributed text + super.attributedText = nil + super.text = newValue + return + } + + accessibilityCustomActions = [] + + //create the primary string + let mutableText = NSMutableAttributedString.mutableText(for: newValue, + textStyle: textStyle, + useScaledFont: useScaledFont, + textColor: textColorConfiguration.getColor(self), + alignment: textAlignment, + lineBreakMode: .byWordWrapping) + + applyAttributes(mutableText) + + // Set attributed text to match typography + super.attributedText = mutableText + + } + + private func styleAttributedText(_ newValue: NSAttributedString?) { + defer { invalidateIntrinsicContentSize() } + guard let newValue = newValue else { + // We don't need any additional styling + super.attributedText = newValue + return + } + + let mutableText = NSMutableAttributedString(attributedString: newValue) + + applyAttributes(mutableText) + + // Modify attributed text to match typography + super.attributedText = mutableText + } + + private func applyAttributes(_ mutableAttributedString: NSMutableAttributedString) { + if let textAttributes { + mutableAttributedString.apply(attributes: textAttributes) } } } -