// // LabelAttributeFont.swift // VDS // // Created by Matt Bruce on 8/3/22. // import Foundation import UIKit public struct TextStyleLabelAttribute: LabelAttributeModel { //-------------------------------------------------- // MARK: - Equatable //-------------------------------------------------- public func isEqual(_ equatable: TextStyleLabelAttribute) -> Bool { return id == equatable.id && range == equatable.range && textColor == equatable.textColor && textStyle == equatable.textStyle } //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- public var id = UUID() public var location: Int public var length: Int public var textStyle: TextStyle public var textColor: UIColor public var textPosition: TextPosition public var lineBreakMode: NSLineBreakMode //-------------------------------------------------- // MARK: - Initializer //-------------------------------------------------- public init(location: Int, length: Int, textStyle: TextStyle, textColor: UIColor = .black, textPosition: TextPosition = .left, lineBreakMode: NSLineBreakMode = .byWordWrapping) { self.location = location self.length = length self.textStyle = textStyle self.textColor = textColor self.textPosition = textPosition self.lineBreakMode = lineBreakMode } public func setAttribute(on attributedString: NSMutableAttributedString) { } } extension Label { internal func apply(attribute: TextStyleLabelAttribute, on attributedString: NSMutableAttributedString){ attributedString.removeAttribute(.font, range: attribute.range) attributedString.removeAttribute(.foregroundColor, range: attribute.range) attributedString.addAttribute(.font, value: attribute.textStyle.font, range: attribute.range) attributedString.addAttribute(.foregroundColor, value: attribute.textColor, range: attribute.range) //set letterSpacing if attribute.textStyle.letterSpacing > 0.0 { attributedString.removeAttribute(.kern, range: attribute.range) attributedString.addAttribute(.kern, value: attribute.textStyle.letterSpacing, range: attribute.range) } //set lineHeight if attribute.textStyle.lineHeight > 0.0 { let lineHeight = attribute.textStyle.lineHeight let adjustment = lineHeight > attribute.textStyle.font.lineHeight ? 2.0 : 1.0 let baselineOffset = (lineHeight - attribute.textStyle.font.lineHeight) / 2.0 / adjustment let paragraph = NSMutableParagraphStyle().with { $0.maximumLineHeight = lineHeight $0.minimumLineHeight = lineHeight $0.alignment = attribute.textPosition.textAlignment $0.lineBreakMode = attribute.lineBreakMode } attributedString.removeAttribute(.baselineOffset, range: attribute.range) attributedString.removeAttribute(.paragraphStyle, range: attribute.range) attributedString.addAttribute(.baselineOffset, value: baselineOffset, range: attribute.range) attributedString.addAttribute(.paragraphStyle, value: paragraph, range: attribute.range) } else if attribute.textPosition != .left { let paragraph = NSMutableParagraphStyle().with { $0.alignment = attribute.textPosition.textAlignment $0.lineBreakMode = attribute.lineBreakMode } attributedString.removeAttribute(.paragraphStyle, range: attribute.range) attributedString.addAttribute(.paragraphStyle, value: paragraph, range: attribute.range) } } }