refactor TextStyle for edgeInsets and removed testing vars

refactor the string helper to remove these tests

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-07-20 12:55:23 -05:00
parent 0460e10b89
commit e12de0c108
2 changed files with 18 additions and 33 deletions

View File

@ -20,40 +20,28 @@ extension NSAttributedString {
extension NSMutableAttributedString {
public static func mutableText(for text: String, textStyle: TextStyle, textColor: UIColor, alignment: NSTextAlignment = .left, lineBreakMode: NSLineBreakMode) -> NSMutableAttributedString {
let font = textStyle.font
let startingAttributes = [NSAttributedString.Key.font: 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)
}
//create the paragraph for specific properties
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = alignment
paragraph.lineBreakMode = lineBreakMode
if textStyle.lineSpacing > 0 {
paragraph.lineSpacing = textStyle.lineSpacing
}
//set lineHeight
if textStyle.lineHeight > 0.0 {
let lineHeight = textStyle.lineHeight
// if textStyle.lineHeight > textStyle.pointSize {
paragraph.maximumLineHeight = lineHeight
paragraph.minimumLineHeight = lineHeight
// paragraph.lineHeightMultiple = lineHeight / textStyle.pointSize
// } else {
// paragraph.lineHeightMultiple = lineHeight / font.pointSize
// }
paragraph.maximumLineHeight = textStyle.lineHeight
}
attributedString.addAttribute( .baselineOffset, value: textStyle.baselineOffset, range: entireRange)
attributedString.addAttribute( .paragraphStyle, value: paragraph, range: entireRange)
return attributedString
//create the attributeArray
var attributes: [NSAttributedString.Key : Any] = [.font: textStyle.font,
.foregroundColor: textColor,
.paragraphStyle: paragraph]
//set letterSpacing
if textStyle.letterSpacing > 0.0 {
attributes[.kern] = textStyle.letterSpacing
}
return NSMutableAttributedString(string: text, attributes: attributes)
}
}

View File

@ -29,8 +29,7 @@ public struct TextStyle: Equatable, RawRepresentable {
public let lineHeight: CGFloat
public let letterSpacing: CGFloat
public let fontFace: Font
public let lineSpacing: CGFloat
public let baselineOffset: CGFloat
public let edgeInsets: UIEdgeInsets
public init?(rawValue: String) {
guard let style = TextStyle.textStyle(for: rawValue) else { return nil }
@ -39,18 +38,16 @@ public struct TextStyle: Equatable, RawRepresentable {
self.lineHeight = style.lineHeight
self.letterSpacing = style.letterSpacing
self.fontFace = style.fontFace
self.lineSpacing = style.lineSpacing
self.baselineOffset = style.baselineOffset
self.edgeInsets = style.edgeInsets
}
public init(rawValue: String, fontFace: Font, pointSize: CGFloat = 0.0, lineHeight: CGFloat = 0.0, letterSpacing: CGFloat = 0.0, lineSpacing: CGFloat = 0.0, baselineOffset: CGFloat = 0.0) {
public init(rawValue: String, fontFace: Font, pointSize: CGFloat = 0.0, lineHeight: CGFloat = 0.0, letterSpacing: CGFloat = 0.0, edgeInsets: UIEdgeInsets = .zero) {
self.rawValue = rawValue
self.fontFace = fontFace
self.pointSize = pointSize
self.lineHeight = lineHeight
self.letterSpacing = letterSpacing
self.lineSpacing = lineSpacing
self.baselineOffset = baselineOffset
self.edgeInsets = edgeInsets
}
public var isBold: Bool {