vds_ios/VDS/Extensions/NSAttributedString.swift
Matt Bruce 2d0252311a updated attributedString
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2023-07-21 10:44:06 -05:00

56 lines
1.8 KiB
Swift

//
// 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, useScaledFont: Bool = true, textColor: UIColor, alignment: NSTextAlignment = .left, lineBreakMode: NSLineBreakMode) -> NSMutableAttributedString {
//create the paragraph for specific properties
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = alignment
paragraph.lineBreakMode = lineBreakMode
var defaultFont = textStyle.font
var lineHeight = textStyle.lineHeight
if useScaledFont {
lineHeight = textStyle.scaledLineHeight
defaultFont = defaultFont.scaledFont
}
//set lineHeight
if textStyle.lineHeight > 0.0 {
paragraph.maximumLineHeight = lineHeight
}
//create the attributeArray
var attributes: [NSAttributedString.Key : Any] = [.font: defaultFont,
.foregroundColor: textColor,
.paragraphStyle: paragraph]
//set letterSpacing
if textStyle.letterSpacing > 0.0 {
attributes[.kern] = textStyle.letterSpacing
}
return NSMutableAttributedString(string: text, attributes: attributes)
}
}