From c150f1552bbc3abd8baa4dc80c149e9d7b6848fb Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Thu, 10 Nov 2022 14:44:13 -0600 Subject: [PATCH] added escape hatch for using a user set attributedText directly to the label Signed-off-by: Matt Bruce --- VDS/Components/Label/Label.swift | 78 +++++++++++++++++--------------- 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/VDS/Components/Label/Label.swift b/VDS/Components/Label/Label.swift index 449c9c80..2b195de8 100644 --- a/VDS/Components/Label/Label.swift +++ b/VDS/Components/Label/Label.swift @@ -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)