updated TextView to mimic label/buttonbase

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-03-13 09:09:45 -05:00
parent 3c375bea39
commit 10fdd6bcb8

View File

@ -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)
}
}
}