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.
open var labelAttributedText: NSAttributedString? {
didSet {
label.useAttributedText = !(labelAttributedText?.string.isEmpty ?? true)
label.attributedText = labelAttributedText
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.
open var childAttributedText: NSAttributedString? {
didSet {
childLabel.useAttributedText = !(childAttributedText?.string.isEmpty ?? true)
childLabel.attributedText = childAttributedText
setNeedsUpdate()
}

View File

@ -42,6 +42,13 @@ open class Label: UILabel, ViewProtocol, UserInfoable {
//--------------------------------------------------
// MARK: - Private Properties
//--------------------------------------------------
private enum TextSetMode {
case text
case attributedText
}
private var textSetMode: TextSetMode = .text
private var initialSetupPerformed = false
private var edgeInsets: UIEdgeInsets { textStyle.edgeInsets }
@ -103,10 +110,6 @@ open class Label: UILabel, ViewProtocol, UserInfoable {
/// Key of whether or not updateView() is called in setNeedsUpdate()
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.
open var useScaledFont: Bool = false { didSet { setNeedsUpdate() }}
@ -132,16 +135,26 @@ open class Label: UILabel, ViewProtocol, UserInfoable {
private var _text: String?
/// Text that will be used in the label.
override open var text: String? {
get { _text }
override public var text: String! {
get { super.text }
set {
if _text != newValue || newValue != attributedText?.string {
_text = newValue
useAttributedText = false
// 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
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)
}
}
/// Whether the View is enabled or not.
@ -200,22 +213,7 @@ open class Label: UILabel, ViewProtocol, UserInfoable {
}
open func updateView() {
if !useAttributedText {
if let text {
accessibilityCustomActions = []
//create the primary string
let mutableText = NSMutableAttributedString.mutableText(for: text,
textStyle: textStyle,
useScaledFont: useScaledFont,
textColor: textColorConfiguration.getColor(self),
alignment: textAlignment,
lineBreakMode: lineBreakMode)
applyAttributes(mutableText)
//set the attributed text
attributedText = mutableText
restyleText()
//force a drawText
setNeedsDisplay()
@ -223,8 +221,6 @@ open class Label: UILabel, ViewProtocol, UserInfoable {
setNeedsLayout()
layoutIfNeeded()
}
}
}
open func updateAccessibility() {
accessibilityLabel = text
@ -269,6 +265,56 @@ open class Label: UILabel, ViewProtocol, UserInfoable {
//--------------------------------------------------
// 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) {
actions = []

View File

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