From aab0cd86accc11ac1bcdd784f7b13a4ddef25224 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Tue, 16 Aug 2022 16:11:07 -0500 Subject: [PATCH] refactored label to include lineheight and letterspacing Signed-off-by: Matt Bruce --- VDS/Components/Label/Label.swift | 57 ++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/VDS/Components/Label/Label.swift b/VDS/Components/Label/Label.swift index d8231647..531176a0 100644 --- a/VDS/Components/Label/Label.swift +++ b/VDS/Components/Label/Label.swift @@ -128,7 +128,7 @@ open class LabelBase: 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: 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: 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