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) + } + } + }