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

@ -28,6 +28,8 @@ open class LabelBase: UILabel, Handlerable, ViewProtocol, Resettable {
//-------------------------------------------------- //--------------------------------------------------
private var initialSetupPerformed = false private var initialSetupPerformed = false
open var useAttributedText: Bool = false
open var surface: Surface = .light { didSet { didChange() }} open var surface: Surface = .light { didSet { didChange() }}
open var disabled: Bool = false { didSet { isEnabled = !disabled } } open var disabled: Bool = false { didSet { isEnabled = !disabled } }
@ -119,6 +121,7 @@ open class LabelBase: UILabel, Handlerable, ViewProtocol, Resettable {
// MARK: - Overrides // MARK: - Overrides
//-------------------------------------------------- //--------------------------------------------------
open func updateView() { open func updateView() {
if !useAttributedText {
textAlignment = textPosition.textAlignment textAlignment = textPosition.textAlignment
textColor = textColorConfiguration.getColor(self) textColor = textColorConfiguration.getColor(self)
font = typograpicalStyle.font font = typograpicalStyle.font
@ -126,7 +129,6 @@ open class LabelBase: UILabel, Handlerable, ViewProtocol, Resettable {
if let text = text, let font = font, let textColor = textColor { if let text = text, let font = font, let textColor = textColor {
//clear the arrays holding actions //clear the arrays holding actions
accessibilityCustomActions = [] accessibilityCustomActions = []
actions = []
//create the primary string //create the primary string
let startingAttributes = [NSAttributedString.Key.font: font, NSAttributedString.Key.foregroundColor: textColor] let startingAttributes = [NSAttributedString.Key.font: font, NSAttributedString.Key.foregroundColor: textColor]
@ -135,12 +137,24 @@ open class LabelBase: UILabel, Handlerable, ViewProtocol, Resettable {
//set the local lineHeight/lineSpacing attributes //set the local lineHeight/lineSpacing attributes
setStyleAttributes(attributedString: mutableText) setStyleAttributes(attributedString: mutableText)
applyAttributes(mutableText)
//set the attributed text
attributedText = mutableText
}
}
}
// MARK: - Private Attributes
private func applyAttributes(_ mutableAttributedString: NSMutableAttributedString) {
actions = []
if let attributes = attributes { if let attributes = attributes {
//loop through the models attributes //loop through the models attributes
for attribute in attributes { for attribute in attributes {
//add attribute on the string //add attribute on the string
attribute.setAttribute(on: mutableText) attribute.setAttribute(on: mutableAttributedString)
//see if the attribute is Actionable //see if the attribute is Actionable
if let actionable = attribute as? any ActionLabelAttributeModel{ if let actionable = attribute as? any ActionLabelAttributeModel{
@ -152,16 +166,8 @@ open class LabelBase: UILabel, Handlerable, ViewProtocol, Resettable {
} }
} }
} }
//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) { private func setStyleAttributes(attributedString: NSMutableAttributedString) {
//get the range //get the range
let entireRange = NSRange(location: 0, length: attributedString.length) let entireRange = NSRange(location: 0, length: attributedString.length)