refactored label to include lineheight and letterspacing

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-08-16 16:11:07 -05:00
parent b4b1de52a2
commit aab0cd86ac

View File

@ -128,7 +128,7 @@ open class LabelBase<ModelType: LabelModel>: UILabel, ModelHandlerable, ViewProt
font = TypographicalStyle.defaultStyle.font font = TypographicalStyle.defaultStyle.font
} }
if let attributes = viewModel.attributes, let text = viewModel.text, let font = font, let textColor = textColor { if let text = viewModel.text, let font = font, let textColor = textColor {
//clear the arrays holding actions //clear the arrays holding actions
accessibilityCustomActions = [] accessibilityCustomActions = []
actions = [] actions = []
@ -137,19 +137,24 @@ open class LabelBase<ModelType: LabelModel>: UILabel, ModelHandlerable, ViewProt
let startingAttributes = [NSAttributedString.Key.font: font, NSAttributedString.Key.foregroundColor: textColor] let startingAttributes = [NSAttributedString.Key.font: font, NSAttributedString.Key.foregroundColor: textColor]
let mutableText = NSMutableAttributedString(string: text, attributes: startingAttributes) let mutableText = NSMutableAttributedString(string: text, attributes: startingAttributes)
//loop through the models attributes //set the local lineHeight/lineSpacing attributes
for attribute in attributes { setStyleAttributes(viewModel: viewModel, attributedString: mutableText)
//add attribute on the string if let attributes = viewModel.attributes {
attribute.setAttribute(on: mutableText) //loop through the models attributes
for attribute in attributes {
//see if the attribute is Actionable //add attribute on the string
if let actionable = attribute as? any LabelAttributeActionable{ attribute.setAttribute(on: mutableText)
//create a accessibleAction
let customAccessibilityAction = customAccessibilityAction(range: actionable.range)
//create a wrapper for the attributes range, block and //see if the attribute is Actionable
actions.append(LabelAction(range: actionable.range, actionBlock: actionable.action, accessibilityID: customAccessibilityAction?.hashValue ?? -1)) if let actionable = attribute as? any LabelAttributeActionable{
//create a accessibleAction
let customAccessibilityAction = customAccessibilityAction(range: actionable.range)
//create a wrapper for the attributes range, block and
actions.append(LabelAction(range: actionable.range, actionBlock: actionable.action, accessibilityID: customAccessibilityAction?.hashValue ?? -1))
}
} }
} }
@ -163,6 +168,30 @@ open class LabelBase<ModelType: LabelModel>: UILabel, ModelHandlerable, ViewProt
} }
} }
// MARK: - Private Attributes
private func setStyleAttributes(viewModel: ModelType, attributedString: NSMutableAttributedString) {
//get the range
let entireRange = NSRange(location: 0, length: attributedString.length)
//set letterSpacing
if viewModel.typograpicalStyle.letterSpacing > 0.0 {
attributedString.addAttribute(.kern, value: viewModel.typograpicalStyle.letterSpacing, range: entireRange)
}
//set lineHeight
if viewModel.typograpicalStyle.lineHeight > 0.0 {
let lineHeight = viewModel.typograpicalStyle.lineHeight
let adjustment = lineHeight > font.lineHeight ? 2.0 : 1.0
let baselineOffset = (lineHeight - font.lineHeight) / 2.0 / adjustment
let paragraph = NSMutableParagraphStyle().with {
$0.maximumLineHeight = lineHeight
$0.minimumLineHeight = lineHeight
}
attributedString.addAttribute(.baselineOffset, value: baselineOffset, range: entireRange)
attributedString.addAttribute( .paragraphStyle, value: paragraph, range: entireRange)
}
}
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Actionable // MARK: - Actionable
//-------------------------------------------------- //--------------------------------------------------