refactored to use new extension
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
58388b81b8
commit
94fffc289a
@ -169,38 +169,17 @@ open class ButtonBase: UIButton, Buttonable, Handlerable, ViewProtocol, Resettab
|
|||||||
// MARK: - PRIVATE
|
// MARK: - PRIVATE
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
private func updateLabel() {
|
private func updateLabel() {
|
||||||
let font = textStyle.font
|
|
||||||
|
|
||||||
//clear the arrays holding actions
|
//clear the arrays holding actions
|
||||||
accessibilityCustomActions = []
|
accessibilityCustomActions = []
|
||||||
|
|
||||||
//create the primary string
|
//create the primary string
|
||||||
let startingAttributes = [NSAttributedString.Key.font: font, NSAttributedString.Key.foregroundColor: textColor]
|
let mutableText = NSMutableAttributedString.mutableText(for: text ?? "No Text",
|
||||||
let mutableText = NSMutableAttributedString(string: text ?? "No Text", attributes: startingAttributes)
|
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 {
|
if let attributes = attributes {
|
||||||
//loop through the models attributes
|
//loop through the models attributes
|
||||||
for attribute in attributes {
|
for attribute in attributes {
|
||||||
|
|||||||
@ -110,20 +110,15 @@ open class Label: UILabel, Handlerable, ViewProtocol, Resettable, UserInfoable {
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
open func updateView() {
|
open func updateView() {
|
||||||
if !useAttributedText {
|
if !useAttributedText {
|
||||||
textAlignment = textPosition.textAlignment
|
if let text = text {
|
||||||
textColor = textColorConfiguration.getColor(self)
|
|
||||||
font = textStyle.font
|
|
||||||
|
|
||||||
if let text = text, let font = font, let textColor = textColor {
|
|
||||||
accessibilityCustomActions = []
|
accessibilityCustomActions = []
|
||||||
|
|
||||||
//clear the arrays holding actions
|
|
||||||
//create the primary string
|
//create the primary string
|
||||||
let startingAttributes = [NSAttributedString.Key.font: font, NSAttributedString.Key.foregroundColor: textColor]
|
let mutableText = NSMutableAttributedString.mutableText(for: text,
|
||||||
let mutableText = NSMutableAttributedString(string: text, attributes: startingAttributes)
|
textStyle: textStyle,
|
||||||
|
textColor: textColorConfiguration.getColor(self),
|
||||||
//set the local lineHeight/lineSpacing attributes
|
alignment: textPosition.textAlignment,
|
||||||
setStyleAttributes(attributedString: mutableText)
|
lineBreakMode: lineBreakMode)
|
||||||
|
|
||||||
applyAttributes(mutableText)
|
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
|
// MARK: - Actionable
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
|
|||||||
@ -17,3 +17,33 @@ extension NSAttributedString {
|
|||||||
return NSAttributedString(attachment: spacerAttachment)
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user