Skip to content

Commit

Permalink
Merge pull request #24 from kiwicom/fix/font-sizes
Browse files Browse the repository at this point in the history
Fix formatted text font sizing.
  • Loading branch information
PavelHolec authored Feb 10, 2022
2 parents d448d50 + 48636af commit 1214733
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
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
19 changes: 8 additions & 11 deletions Sources/Orbit/Support/TextLinks/TagAttributedStringBuilder.swift
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

0 comments on commit 1214733

Please sign in to comment.