added extension for NSMutableAttributedString

refactoctored code to use extension

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-11-07 08:17:46 -06:00
parent 94bdb22fbd
commit e0540a68ce
3 changed files with 14 additions and 11 deletions

View File

@ -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

View File

@ -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) }
}
}

View File

@ -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)
}
}