From 48636afa99139a5bb5906c0042f273d031da5803 Mon Sep 17 00:00:00 2001 From: Pavel Holec Date: Thu, 10 Feb 2022 16:11:54 +0100 Subject: [PATCH] Fix formatted text font sizing. --- Sources/Orbit/Components/Text.swift | 27 ++++++++++++++----- .../TagAttributedStringBuilder.swift | 19 ++++++------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/Sources/Orbit/Components/Text.swift b/Sources/Orbit/Components/Text.swift index 70c08dfc277..a8f9b1a74d1 100644 --- a/Sources/Orbit/Components/Text.swift +++ b/Sources/Orbit/Components/Text.swift @@ -71,7 +71,7 @@ public struct Text: View { var textLinkContent: NSAttributedString { TagAttributedStringBuilder.all.attributedStringForLinks( content, - fontSize: scaledSize, + fontSize: size.value, fontWeight: weight, lineSpacing: lineSpacing, alignment: alignment @@ -85,7 +85,7 @@ public struct Text: View { var attributedText: NSAttributedString { TagAttributedStringBuilder.all.attributedString( content, - fontSize: scaledSize, + fontSize: size.value, fontWeight: weight, lineSpacing: lineSpacing, color: foregroundColor, @@ -228,12 +228,15 @@ struct TextPreviews: PreviewProvider { .previewDisplayName("Text - formatted") Group { - Text(multilineText, color : .none) - .foregroundColor(.blueDark) + Text(multilineText) .previewDisplayName("Text - multiline") Text(multilineFormattedText) .previewDisplayName("Text - formatted multiline") + + Text(multilineFormattedText, color: .none) + .foregroundColor(.blueDark) + .previewDisplayName("Text - formatted multiline with color override") Text(multilineText, alignment: .trailing) .previewDisplayName("Text - multiline") @@ -279,6 +282,18 @@ struct TextPreviews: PreviewProvider { Separator() + Group { + Text("Text custom size", size: .custom(5)) + Text("Text custom size", size: .custom(21)) + + Text("Text formatted custom size", size: .custom(5)) + Text("Text formatted custom size", size: .custom(21)) + + Text("Text TextLink custom size", size: .custom(5)) + Text("Text TextLink custom size", size: .custom(21)) + Separator() + } + Group { Text("Text Normal - Undefined color, modified to Blue", size: .normal, color: nil) .foregroundColor(.blueNormal) @@ -289,10 +304,10 @@ struct TextPreviews: PreviewProvider { Text("Text Normal - Custom color", size: .normal, color: .custom(.productDark)) .foregroundColor(.blueNormal) .foregroundColor(.redNormal) + + Separator() } - Separator() - Group { Text("Text Normal, M") .environment(\.sizeCategory, .medium) diff --git a/Sources/Orbit/Support/TextLinks/TagAttributedStringBuilder.swift b/Sources/Orbit/Support/TextLinks/TagAttributedStringBuilder.swift index 4a02f14a394..1d0d8bc4861 100644 --- a/Sources/Orbit/Support/TextLinks/TagAttributedStringBuilder.swift +++ b/Sources/Orbit/Support/TextLinks/TagAttributedStringBuilder.swift @@ -69,7 +69,7 @@ final class TagAttributedStringBuilder { func attributedString( _ string: String, - fontSize: CGFloat? = nil, + fontSize: CGFloat, fontWeight: Font.Weight = .regular, lineSpacing: CGFloat?, color: UIColor? = nil, @@ -84,7 +84,7 @@ final class TagAttributedStringBuilder { textAttributes[.paragraphStyle] = titleParagraphStyle } - textAttributes[.font] = fontSize.map { UIFont.orbit(size: $0, weight: fontWeight.uiKit) } + textAttributes[.font] = UIFont.orbit(size: fontSize, weight: fontWeight.uiKit) textAttributes[.foregroundColor] = color var linksAttributes: [NSAttributedString.Key: Any] = [:] @@ -223,8 +223,7 @@ private extension TagAttributedStringBuilder.Tag { switch self { case .anchor, .applink: guard result.ranges.count == 3, - let font = textAttributes[.font] as? UIFont, - let size = UIFont.Size(rawValue: Int(font.pointSize)) + let font = textAttributes[.font] as? UIFont else { return nil } @@ -233,7 +232,7 @@ private extension TagAttributedStringBuilder.Tag { let attributes = [ .link: url, - .font: UIFont.orbit(size: size, weight: .medium), + .font: UIFont.orbit(size: font.pointSize, weight: .medium), ].merging(tagTextAttributes, uniquingKeysWith: { $1 }) return stringByAddingAttributes(attributes, to: currentAttributedString, at: result.ranges[2]) @@ -244,9 +243,8 @@ private extension TagAttributedStringBuilder.Tag { if let font = tagTextAttributes[.font] as? UIFont { boldFont = font - } else if let font = textAttributes[.font] as? UIFont, - let size = UIFont.Size(rawValue: Int(font.pointSize)) { - boldFont = .orbit(size: size, weight: .bold) + } else if let font = textAttributes[.font] as? UIFont { + boldFont = .orbit(size: font.pointSize, weight: .bold) } else { boldFont = .orbit(size: .normal, weight: .bold) } @@ -267,14 +265,13 @@ private extension TagAttributedStringBuilder.Tag { case .ref: guard result.ranges.count == 2, let color = tagTextAttributes[.foregroundColor] as? UIColor, - let font = textAttributes[.font] as? UIFont, - let size = UIFont.Size(rawValue: Int(font.pointSize)) + let font = textAttributes[.font] as? UIFont else { return nil } return stringByAddingAttributes( - [.foregroundColor: color, .font: UIFont.orbit(size: size, weight: .bold)], + [.foregroundColor: color, .font: UIFont.orbit(size: font.pointSize, weight: .bold)], to: currentAttributedString, at: result.ranges[1] )