// // NSAttributedString.swift // VDS // // Created by Matt Bruce on 5/30/23. // import Foundation import UIKit extension NSAttributedString { public static func spacer(for width: CGFloat) -> NSAttributedString { let spacerImage = UIImage() let spacerAttachment = NSTextAttachment() spacerAttachment.bounds = .init(x: 0, y: 0, width: width, height: 1) spacerAttachment.image = spacerImage return NSAttributedString(attachment: spacerAttachment) } } extension NSMutableAttributedString { public static func mutableText(for text: String, textStyle: TextStyle, textColor: UIColor, alignment: NSTextAlignment = .left, lineBreakMode: NSLineBreakMode) -> NSMutableAttributedString { let startingAttributes = [NSAttributedString.Key.font: textStyle.font, NSAttributedString.Key.foregroundColor: textColor] let attributedString = NSMutableAttributedString(string: text, attributes: startingAttributes) //get the range let entireRange = NSRange(location: 0, length: attributedString.length) //set letterSpacing if textStyle.letterSpacing > 0.0 { attributedString.addAttribute(.kern, value: textStyle.letterSpacing, range: entireRange) } let paragraph = NSMutableParagraphStyle() paragraph.alignment = alignment paragraph.lineBreakMode = lineBreakMode //set lineHeight if textStyle.lineHeight > 0.0 { let lineHeight = textStyle.lineHeight paragraph.maximumLineHeight = lineHeight paragraph.minimumLineHeight = lineHeight } attributedString.addAttribute( .paragraphStyle, value: paragraph, range: entireRange) return attributedString } }