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 { extension NSMutableAttributedString {
public static func mutableText(for text: String, textStyle: TextStyle, textColor: UIColor, alignment: NSTextAlignment = .left, lineBreakMode: NSLineBreakMode) -> 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 //create the paragraph for specific properties
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() let paragraph = NSMutableParagraphStyle()
paragraph.alignment = alignment paragraph.alignment = alignment
paragraph.lineBreakMode = lineBreakMode paragraph.lineBreakMode = lineBreakMode
if textStyle.lineSpacing > 0 {
paragraph.lineSpacing = textStyle.lineSpacing
}
//set lineHeight //set lineHeight
if textStyle.lineHeight > 0.0 { if textStyle.lineHeight > 0.0 {
let lineHeight = textStyle.lineHeight paragraph.maximumLineHeight = textStyle.lineHeight
// if textStyle.lineHeight > textStyle.pointSize {
paragraph.maximumLineHeight = lineHeight
paragraph.minimumLineHeight = lineHeight
// paragraph.lineHeightMultiple = lineHeight / textStyle.pointSize
// } else {
// paragraph.lineHeightMultiple = lineHeight / font.pointSize
// }
} }
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 lineHeight: CGFloat
public let letterSpacing: CGFloat public let letterSpacing: CGFloat
public let fontFace: Font public let fontFace: Font
public let lineSpacing: CGFloat public let edgeInsets: UIEdgeInsets
public let baselineOffset: CGFloat
public init?(rawValue: String) { public init?(rawValue: String) {
guard let style = TextStyle.textStyle(for: rawValue) else { return nil } guard let style = TextStyle.textStyle(for: rawValue) else { return nil }
@ -39,18 +38,16 @@ public struct TextStyle: Equatable, RawRepresentable {
self.lineHeight = style.lineHeight self.lineHeight = style.lineHeight
self.letterSpacing = style.letterSpacing self.letterSpacing = style.letterSpacing
self.fontFace = style.fontFace self.fontFace = style.fontFace
self.lineSpacing = style.lineSpacing self.edgeInsets = style.edgeInsets
self.baselineOffset = style.baselineOffset
} }
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.rawValue = rawValue
self.fontFace = fontFace self.fontFace = fontFace
self.pointSize = pointSize self.pointSize = pointSize
self.lineHeight = lineHeight self.lineHeight = lineHeight
self.letterSpacing = letterSpacing self.letterSpacing = letterSpacing
self.lineSpacing = lineSpacing self.edgeInsets = edgeInsets
self.baselineOffset = baselineOffset
} }
public var isBold: Bool { public var isBold: Bool {