From e12de0c1082128ae6ee149936c00c93de0b5fccc Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Thu, 20 Jul 2023 12:55:23 -0500 Subject: [PATCH] refactor TextStyle for edgeInsets and removed testing vars refactor the string helper to remove these tests Signed-off-by: Matt Bruce --- VDS/Extensions/NSAttributedString.swift | 40 +++++++++---------------- VDS/Typography/Typography.swift | 11 +++---- 2 files changed, 18 insertions(+), 33 deletions(-) diff --git a/VDS/Extensions/NSAttributedString.swift b/VDS/Extensions/NSAttributedString.swift index ec36fe67..814f3c98 100644 --- a/VDS/Extensions/NSAttributedString.swift +++ b/VDS/Extensions/NSAttributedString.swift @@ -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) + } } diff --git a/VDS/Typography/Typography.swift b/VDS/Typography/Typography.swift index 902eb09e..de0a9289 100644 --- a/VDS/Typography/Typography.swift +++ b/VDS/Typography/Typography.swift @@ -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 {