From e0540a68ce957dfb60c735e33d5a75fda004bdc1 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Tue, 7 Nov 2023 08:17:46 -0600 Subject: [PATCH] added extension for NSMutableAttributedString refactoctored code to use extension Signed-off-by: Matt Bruce --- VDS/Components/Buttons/ButtonBase.swift | 7 ++----- .../Label/Attributes/LabelAttributeModel.swift | 11 +++++++++++ VDS/Components/Label/Label.swift | 7 +------ 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/VDS/Components/Buttons/ButtonBase.swift b/VDS/Components/Buttons/ButtonBase.swift index 17439074..255c6f64 100644 --- a/VDS/Components/Buttons/ButtonBase.swift +++ b/VDS/Components/Buttons/ButtonBase.swift @@ -167,12 +167,9 @@ open class ButtonBase: UIButton, ViewProtocol, UserInfoable, Clickable { alignment: titleLabel?.textAlignment ?? .center, lineBreakMode: titleLabel?.lineBreakMode ?? .byTruncatingTail) + //apply any attributes if let attributes = textAttributes { - //loop through the models attributes - for attribute in attributes { - //add attribute on the string - attribute.setAttribute(on: mutableText) - } + mutableText.apply(attributes: attributes) } //set the attributed text diff --git a/VDS/Components/Label/Attributes/LabelAttributeModel.swift b/VDS/Components/Label/Attributes/LabelAttributeModel.swift index 84fdc005..62e1bbd8 100644 --- a/VDS/Components/Label/Attributes/LabelAttributeModel.swift +++ b/VDS/Components/Label/Attributes/LabelAttributeModel.swift @@ -54,3 +54,14 @@ public extension NSAttributedString { return TextStyleLabelAttribute(location: range.location, length: range.length, textStyle: style) } } + +extension NSMutableAttributedString { + public func apply(attribute: any LabelAttributeModel) { + attribute.setAttribute(on: self) + } + + public func apply(attributes: [any LabelAttributeModel]) { + attributes.forEach { apply(attribute: $0) } + } +} + diff --git a/VDS/Components/Label/Label.swift b/VDS/Components/Label/Label.swift index 0e804b30..7ecb79cd 100644 --- a/VDS/Components/Label/Label.swift +++ b/VDS/Components/Label/Label.swift @@ -273,12 +273,7 @@ open class Label: UILabel, ViewProtocol, UserInfoable { actions = [] if let attributes = attributes { - //loop through the models attributes - for attribute in attributes { - - //add attribute on the string - attribute.setAttribute(on: mutableAttributedString) - } + mutableAttributedString.apply(attributes: attributes) } }