refactored to use new extension

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-06-19 14:52:48 -05:00
parent 58388b81b8
commit 94fffc289a
3 changed files with 42 additions and 61 deletions

View File

@ -169,38 +169,17 @@ open class ButtonBase: UIButton, Buttonable, Handlerable, ViewProtocol, Resettab
// MARK: - PRIVATE
//--------------------------------------------------
private func updateLabel() {
let font = textStyle.font
//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 ?? "No Text", attributes: startingAttributes)
let mutableText = NSMutableAttributedString.mutableText(for: text ?? "No Text",
textStyle: textStyle,
textColor: textColor,
alignment: titleLabel?.textAlignment ?? .center,
lineBreakMode: titleLabel?.lineBreakMode ?? .byTruncatingTail)
//set the local lineHeight/lineSpacing attributes
//get the range
let entireRange = NSRange(location: 0, length: mutableText.length)
//set letterSpacing
if textStyle.letterSpacing > 0.0 {
mutableText.addAttribute(.kern, value: textStyle.letterSpacing, range: entireRange)
}
let paragraph = NSMutableParagraphStyle().with {
$0.alignment = titleLabel?.textAlignment ?? .center
$0.lineBreakMode = titleLabel?.lineBreakMode ?? .byTruncatingTail
}
//set lineHeight
if textStyle.lineHeight > 0.0 {
let lineHeight = textStyle.lineHeight
paragraph.maximumLineHeight = lineHeight
paragraph.minimumLineHeight = lineHeight
}
mutableText.addAttribute( .paragraphStyle, value: paragraph, range: entireRange)
if let attributes = attributes {
//loop through the models attributes
for attribute in attributes {

View File

@ -110,20 +110,15 @@ open class Label: UILabel, Handlerable, ViewProtocol, Resettable, UserInfoable {
//--------------------------------------------------
open func updateView() {
if !useAttributedText {
textAlignment = textPosition.textAlignment
textColor = textColorConfiguration.getColor(self)
font = textStyle.font
if let text = text, let font = font, let textColor = textColor {
if let text = text {
accessibilityCustomActions = []
//clear the arrays holding 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)
let mutableText = NSMutableAttributedString.mutableText(for: text,
textStyle: textStyle,
textColor: textColorConfiguration.getColor(self),
alignment: textPosition.textAlignment,
lineBreakMode: lineBreakMode)
applyAttributes(mutableText)
@ -160,30 +155,7 @@ open class Label: UILabel, Handlerable, ViewProtocol, Resettable, UserInfoable {
}
}
}
private func setStyleAttributes(attributedString: NSMutableAttributedString) {
//get the range
let entireRange = NSRange(location: 0, length: attributedString.length)
//set letterSpacing
if textStyle.letterSpacing > 0.0 {
attributedString.addAttribute(.kern, value: textStyle.letterSpacing, range: entireRange)
}
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = textPosition.textAlignment
paragraph.lineBreakMode = lineBreakMode
//set lineHeight
if textStyle.lineHeight > 0.0 {
let lineHeight = textStyle.lineHeight
paragraph.maximumLineHeight = lineHeight
paragraph.minimumLineHeight = lineHeight
}
attributedString.addAttribute( .paragraphStyle, value: paragraph, range: entireRange)
}
//--------------------------------------------------
// MARK: - Actionable
//--------------------------------------------------

View File

@ -17,3 +17,33 @@ extension NSAttributedString {
return NSAttributedString(attachment: spacerAttachment)
}
}
extension NSMutableAttributedString {
public static func mutableText(for text: String, textStyle: TextStyle, textColor: UIColor, alignment: NSTextAlignment = .left, lineBreakMode: NSLineBreakMode) -> NSMutableAttributedString {
let startingAttributes = [NSAttributedString.Key.font: textStyle.font, NSAttributedString.Key.foregroundColor: textColor]
let attributedString = NSMutableAttributedString(string: text, attributes: startingAttributes)
//get the range
let entireRange = NSRange(location: 0, length: attributedString.length)
//set letterSpacing
if textStyle.letterSpacing > 0.0 {
attributedString.addAttribute(.kern, value: textStyle.letterSpacing, range: entireRange)
}
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = alignment
paragraph.lineBreakMode = lineBreakMode
//set lineHeight
if textStyle.lineHeight > 0.0 {
let lineHeight = textStyle.lineHeight
paragraph.maximumLineHeight = lineHeight
paragraph.minimumLineHeight = lineHeight
}
attributedString.addAttribute( .paragraphStyle, value: paragraph, range: entireRange)
return attributedString
}
}