refactored to use new extension
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
58388b81b8
commit
94fffc289a
@ -169,37 +169,16 @@ 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)
|
||||
|
||||
//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)
|
||||
let mutableText = NSMutableAttributedString.mutableText(for: text ?? "No Text",
|
||||
textStyle: textStyle,
|
||||
textColor: textColor,
|
||||
alignment: titleLabel?.textAlignment ?? .center,
|
||||
lineBreakMode: titleLabel?.lineBreakMode ?? .byTruncatingTail)
|
||||
|
||||
if let attributes = attributes {
|
||||
//loop through the models attributes
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -161,29 +156,6 @@ 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
|
||||
//--------------------------------------------------
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user