diff --git a/VDS/Components/Buttons/Button/ButtonBase.swift b/VDS/Components/Buttons/Button/ButtonBase.swift index 60844f89..00eed2db 100644 --- a/VDS/Components/Buttons/Button/ButtonBase.swift +++ b/VDS/Components/Buttons/Button/ButtonBase.swift @@ -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 { diff --git a/VDS/Components/Label/Label.swift b/VDS/Components/Label/Label.swift index 111743d0..0ca91cd0 100644 --- a/VDS/Components/Label/Label.swift +++ b/VDS/Components/Label/Label.swift @@ -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 //-------------------------------------------------- diff --git a/VDS/Extensions/NSAttributedString.swift b/VDS/Extensions/NSAttributedString.swift index d51ec05a..a4b94fb9 100644 --- a/VDS/Extensions/NSAttributedString.swift +++ b/VDS/Extensions/NSAttributedString.swift @@ -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 + } +}