refactored label to include lineheight and letterspacing
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
b4b1de52a2
commit
aab0cd86ac
@ -128,7 +128,7 @@ open class LabelBase<ModelType: LabelModel>: UILabel, ModelHandlerable, ViewProt
|
||||
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
|
||||
accessibilityCustomActions = []
|
||||
actions = []
|
||||
@ -136,23 +136,28 @@ open class LabelBase<ModelType: LabelModel>: UILabel, ModelHandlerable, ViewProt
|
||||
//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(viewModel: viewModel, attributedString: mutableText)
|
||||
|
||||
//loop through the models attributes
|
||||
for attribute in attributes {
|
||||
|
||||
//add attribute on the string
|
||||
attribute.setAttribute(on: mutableText)
|
||||
|
||||
//see if the attribute is Actionable
|
||||
if let actionable = attribute as? any LabelAttributeActionable{
|
||||
//create a accessibleAction
|
||||
let customAccessibilityAction = customAccessibilityAction(range: actionable.range)
|
||||
if let attributes = viewModel.attributes {
|
||||
//loop through the models attributes
|
||||
for attribute in attributes {
|
||||
|
||||
//create a wrapper for the attributes range, block and
|
||||
actions.append(LabelAction(range: actionable.range, actionBlock: actionable.action, accessibilityID: customAccessibilityAction?.hashValue ?? -1))
|
||||
//add attribute on the string
|
||||
attribute.setAttribute(on: mutableText)
|
||||
|
||||
//see if the attribute is Actionable
|
||||
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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//only enabled if enabled and has actions
|
||||
isUserInteractionEnabled = !viewModel.disabled && !actions.isEmpty
|
||||
|
||||
@ -162,6 +167,30 @@ open class LabelBase<ModelType: LabelModel>: UILabel, ModelHandlerable, ViewProt
|
||||
text = viewModel.text
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user