Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix formatted text font sizing. #24

Merged
merged 1 commit into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions Sources/Orbit/Components/Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 <strong>formatted</strong> custom size", size: .custom(5))
Text("Text <strong>formatted</strong> custom size", size: .custom(21))

Text("Text <applink1>TextLink</applink1> custom size", size: .custom(5))
Text("Text <applink1>TextLink</applink1> custom size", size: .custom(21))
Separator()
}

Group {
Text("Text Normal - Undefined color, modified to Blue", size: .normal, color: nil)
.foregroundColor(.blueNormal)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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] = [:]
Expand Down Expand Up @@ -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
}
Expand All @@ -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])
Expand All @@ -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)
}
Expand All @@ -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]
)
Expand Down