From e46017f3150d4d37b022dbf2d9761dd25b093cb3 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Thu, 10 Nov 2022 14:43:02 -0600 Subject: [PATCH] updated FontLabelAttribute for Kerning/line spacing/ line height Signed-off-by: Matt Bruce --- .../Label/Attributes/FontLabelAttribute.swift | 42 ++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/VDS/Components/Label/Attributes/FontLabelAttribute.swift b/VDS/Components/Label/Attributes/FontLabelAttribute.swift index 6456b76c..a1fc2ff2 100644 --- a/VDS/Components/Label/Attributes/FontLabelAttribute.swift +++ b/VDS/Components/Label/Attributes/FontLabelAttribute.swift @@ -24,21 +24,59 @@ public struct FontLabelAttribute: LabelAttributeModel { public var length: Int public var style: TypographicalStyle public var color: UIColor + public var textPosition: TextPosition + public var lineBreakMode: NSLineBreakMode //-------------------------------------------------- // MARK: - Initializer //-------------------------------------------------- - public init(location: Int, length: Int, style: TypographicalStyle, color: UIColor = .black) { + public init(location: Int, length: Int, style: TypographicalStyle, color: UIColor = .black, textPosition: TextPosition = .left, lineBreakMode: NSLineBreakMode = .byWordWrapping) { self.location = location self.length = length self.style = style self.color = color + self.textPosition = textPosition + self.lineBreakMode = lineBreakMode } public func setAttribute(on attributedString: NSMutableAttributedString) { - attributedString.removeAttribute(.font, range: range) attributedString.removeAttribute(.foregroundColor, range: range) attributedString.addAttribute(.font, value: style.font, range: range) attributedString.addAttribute(.foregroundColor, value: color, range: range) + setStyleAttributes(attributedString) } + + private func setStyleAttributes(_ attributedString: NSMutableAttributedString) { + //set letterSpacing + if style.letterSpacing > 0.0 { + attributedString.removeAttribute(.kern, range: range) + attributedString.addAttribute(.kern, value: style.letterSpacing, range: range) + } + + //set lineHeight + if style.lineHeight > 0.0 { + let lineHeight = style.lineHeight + let adjustment = lineHeight > style.font.lineHeight ? 2.0 : 1.0 + let baselineOffset = (lineHeight - style.font.lineHeight) / 2.0 / adjustment + let paragraph = NSMutableParagraphStyle().with { + $0.maximumLineHeight = lineHeight + $0.minimumLineHeight = lineHeight + $0.alignment = textPosition.textAlignment + $0.lineBreakMode = lineBreakMode + } + attributedString.removeAttribute(.baselineOffset, range: range) + attributedString.removeAttribute(.paragraphStyle, range: range) + attributedString.addAttribute(.baselineOffset, value: baselineOffset, range: range) + attributedString.addAttribute(.paragraphStyle, value: paragraph, range: range) + + } else if textPosition != .left { + let paragraph = NSMutableParagraphStyle().with { + $0.alignment = textPosition.textAlignment + $0.lineBreakMode = lineBreakMode + } + attributedString.removeAttribute(.paragraphStyle, range: range) + attributedString.addAttribute(.paragraphStyle, value: paragraph, range: range) + } + } + }