updated to fix issue for attributedText

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-03-08 09:20:17 -06:00
parent 8f320a9363
commit 441971d2f9
3 changed files with 82 additions and 41 deletions

View File

@ -102,7 +102,6 @@ open class SelectorItemBase<Selector: SelectorControlable>: Control, Errorable,
/// Instead of use labelText and labelTextAttirbutes, this is a fully baked NSAttributedString with both text and attributes. /// Instead of use labelText and labelTextAttirbutes, this is a fully baked NSAttributedString with both text and attributes.
open var labelAttributedText: NSAttributedString? { open var labelAttributedText: NSAttributedString? {
didSet { didSet {
label.useAttributedText = !(labelAttributedText?.string.isEmpty ?? true)
label.attributedText = labelAttributedText label.attributedText = labelAttributedText
setNeedsUpdate() setNeedsUpdate()
} }
@ -117,7 +116,6 @@ open class SelectorItemBase<Selector: SelectorControlable>: Control, Errorable,
/// Instead of use childText and childTextAttirbutes, this is a fully baked NSAttributedString with both text and attributes. /// Instead of use childText and childTextAttirbutes, this is a fully baked NSAttributedString with both text and attributes.
open var childAttributedText: NSAttributedString? { open var childAttributedText: NSAttributedString? {
didSet { didSet {
childLabel.useAttributedText = !(childAttributedText?.string.isEmpty ?? true)
childLabel.attributedText = childAttributedText childLabel.attributedText = childAttributedText
setNeedsUpdate() setNeedsUpdate()
} }

View File

@ -42,6 +42,13 @@ open class Label: UILabel, ViewProtocol, UserInfoable {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Private Properties // MARK: - Private Properties
//-------------------------------------------------- //--------------------------------------------------
private enum TextSetMode {
case text
case attributedText
}
private var textSetMode: TextSetMode = .text
private var initialSetupPerformed = false private var initialSetupPerformed = false
private var edgeInsets: UIEdgeInsets { textStyle.edgeInsets } private var edgeInsets: UIEdgeInsets { textStyle.edgeInsets }
@ -102,10 +109,6 @@ open class Label: UILabel, ViewProtocol, UserInfoable {
//-------------------------------------------------- //--------------------------------------------------
/// Key of whether or not updateView() is called in setNeedsUpdate() /// Key of whether or not updateView() is called in setNeedsUpdate()
open var shouldUpdateView: Bool = true open var shouldUpdateView: Bool = true
/// Determines if the label should use its own attributedText property instead of rendering the attributedText propert
/// based of other local properties, such as textStyle, textColor, surface, etc... The default value is false.
open var useAttributedText: Bool = false
/// Will determine if a scaled font should be used for the font. /// Will determine if a scaled font should be used for the font.
open var useScaledFont: Bool = false { didSet { setNeedsUpdate() }} open var useScaledFont: Bool = false { didSet { setNeedsUpdate() }}
@ -132,15 +135,25 @@ open class Label: UILabel, ViewProtocol, UserInfoable {
private var _text: String? private var _text: String?
/// Text that will be used in the label. /// Text that will be used in the label.
override open var text: String? { override public var text: String! {
get { _text } get { super.text }
set { set {
if _text != newValue || newValue != attributedText?.string { // When text is set, we may need to re-style it as attributedText
_text = newValue // with the correct paragraph style to achieve the desired line height.
useAttributedText = false textSetMode = .text
attributes?.removeAll() attributes?.removeAll()
setNeedsUpdate() styleText(newValue)
} }
}
///AttributedText that will be used in the label.
override public 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)
} }
} }
@ -200,30 +213,13 @@ open class Label: UILabel, ViewProtocol, UserInfoable {
} }
open func updateView() { open func updateView() {
if !useAttributedText { restyleText()
if let text {
accessibilityCustomActions = [] //force a drawText
setNeedsDisplay()
//create the primary string
let mutableText = NSMutableAttributedString.mutableText(for: text, setNeedsLayout()
textStyle: textStyle, layoutIfNeeded()
useScaledFont: useScaledFont,
textColor: textColorConfiguration.getColor(self),
alignment: textAlignment,
lineBreakMode: lineBreakMode)
applyAttributes(mutableText)
//set the attributed text
attributedText = mutableText
//force a drawText
setNeedsDisplay()
setNeedsLayout()
layoutIfNeeded()
}
}
} }
open func updateAccessibility() { open func updateAccessibility() {
@ -269,6 +265,56 @@ open class Label: UILabel, ViewProtocol, UserInfoable {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Private Methods // MARK: - Private Methods
//-------------------------------------------------- //--------------------------------------------------
private func restyleText() {
if textSetMode == .text {
styleText(text)
} else {
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: lineBreakMode)
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
}
var mutableText = NSMutableAttributedString(attributedString: newValue)
applyAttributes(mutableText)
// Modify attributed text to match typography
super.attributedText = newValue
}
private func applyAttributes(_ mutableAttributedString: NSMutableAttributedString) { private func applyAttributes(_ mutableAttributedString: NSMutableAttributedString) {
actions = [] actions = []

View File

@ -100,7 +100,6 @@ open class RadioBoxItem: Control, Changeable, FormFieldable {
/// If provided, the RadioBox textAttributedText will be rendered. /// If provided, the RadioBox textAttributedText will be rendered.
open var textAttributedText: NSAttributedString? { open var textAttributedText: NSAttributedString? {
didSet { didSet {
textLabel.useAttributedText = !(textAttributedText?.string.isEmpty ?? true)
textLabel.attributedText = textAttributedText textLabel.attributedText = textAttributedText
setNeedsUpdate() setNeedsUpdate()
} }
@ -115,7 +114,6 @@ open class RadioBoxItem: Control, Changeable, FormFieldable {
/// If provided, the RadioBox subTextAttributedText will be rendered. /// If provided, the RadioBox subTextAttributedText will be rendered.
open var subTextAttributedText: NSAttributedString? { open var subTextAttributedText: NSAttributedString? {
didSet { didSet {
subTextLabel.useAttributedText = !(subTextAttributedText?.string.isEmpty ?? true)
subTextLabel.attributedText = subTextAttributedText subTextLabel.attributedText = subTextAttributedText
setNeedsUpdate() setNeedsUpdate()
} }
@ -130,7 +128,6 @@ open class RadioBoxItem: Control, Changeable, FormFieldable {
/// If provided, the RadioBox subTextRightAttributedText will be rendered. /// If provided, the RadioBox subTextRightAttributedText will be rendered.
open var subTextRightAttributedText: NSAttributedString? { open var subTextRightAttributedText: NSAttributedString? {
didSet { didSet {
subTextRightLabel.useAttributedText = !(subTextRightAttributedText?.string.isEmpty ?? true)
subTextRightLabel.attributedText = subTextRightAttributedText subTextRightLabel.attributedText = subTextRightAttributedText
setNeedsUpdate() setNeedsUpdate()
} }