added escape hatch for using a user set attributedText directly to the label

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-11-10 14:44:13 -06:00
parent 0b73207bd6
commit c150f1552b

View File

@ -27,6 +27,8 @@ open class LabelBase: UILabel, Handlerable, ViewProtocol, Resettable {
// MARK: - Properties
//--------------------------------------------------
private var initialSetupPerformed = false
open var useAttributedText: Bool = false
open var surface: Surface = .light { didSet { didChange() }}
@ -119,49 +121,53 @@ open class LabelBase: UILabel, Handlerable, ViewProtocol, Resettable {
// MARK: - Overrides
//--------------------------------------------------
open func updateView() {
textAlignment = textPosition.textAlignment
textColor = textColorConfiguration.getColor(self)
font = typograpicalStyle.font
if !useAttributedText {
textAlignment = textPosition.textAlignment
textColor = textColorConfiguration.getColor(self)
font = typograpicalStyle.font
if let text = text, let font = font, let textColor = textColor {
//clear the arrays holding actions
accessibilityCustomActions = []
//create the primary string
let startingAttributes = [NSAttributedString.Key.font: font, NSAttributedString.Key.foregroundColor: textColor]
let mutableText = NSMutableAttributedString(string: text, attributes: startingAttributes)
//set the local lineHeight/lineSpacing attributes
setStyleAttributes(attributedString: mutableText)
applyAttributes(mutableText)
//set the attributed text
attributedText = mutableText
}
}
}
if let text = text, let font = font, let textColor = textColor {
//clear the arrays holding actions
accessibilityCustomActions = []
actions = []
//create the primary string
let startingAttributes = [NSAttributedString.Key.font: font, NSAttributedString.Key.foregroundColor: textColor]
let mutableText = NSMutableAttributedString(string: text, attributes: startingAttributes)
//set the local lineHeight/lineSpacing attributes
setStyleAttributes(attributedString: mutableText)
if let attributes = attributes {
//loop through the models attributes
for attribute in attributes {
// MARK: - Private Attributes
private func applyAttributes(_ mutableAttributedString: NSMutableAttributedString) {
actions = []
if let attributes = attributes {
//loop through the models attributes
for attribute in attributes {
//add attribute on the string
attribute.setAttribute(on: mutableAttributedString)
//see if the attribute is Actionable
if let actionable = attribute as? any ActionLabelAttributeModel{
//create a accessibleAction
let customAccessibilityAction = customAccessibilityAction(range: actionable.range, accessibleText: actionable.accessibleText)
//add attribute on the string
attribute.setAttribute(on: mutableText)
//see if the attribute is Actionable
if let actionable = attribute as? any ActionLabelAttributeModel{
//create a accessibleAction
let customAccessibilityAction = customAccessibilityAction(range: actionable.range, accessibleText: actionable.accessibleText)
//create a wrapper for the attributes range, block and
actions.append(LabelAction(range: actionable.range, action: actionable.action, accessibilityID: customAccessibilityAction?.hashValue ?? -1))
}
//create a wrapper for the attributes range, block and
actions.append(LabelAction(range: actionable.range, action: actionable.action, accessibilityID: customAccessibilityAction?.hashValue ?? -1))
}
}
//only enabled if enabled and has actions
isUserInteractionEnabled = !disabled && !actions.isEmpty
//set the attributed text
attributedText = mutableText
}
}
// MARK: - Private Attributes
private func setStyleAttributes(attributedString: NSMutableAttributedString) {
//get the range
let entireRange = NSRange(location: 0, length: attributedString.length)