Skip to content

Commit fa067b4

Browse files
authored
Merge pull request #8480 from woocommerce/issue/refactor-font-helpers
Unify font helpers
2 parents 6cbabd6 + f42249d commit fa067b4

File tree

2 files changed

+16
-37
lines changed

2 files changed

+16
-37
lines changed

WooCommerce/Classes/Extensions/UIFont+Helpers.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,19 @@ extension UIFont {
8484
return UIFontMetrics(forTextStyle: style).scaledFont(for: noticonFont)
8585
}
8686

87-
/// Returns a UIFont instance for the specified Style + Weight.
87+
/// Returns a UIFont instance for the specified Style + Weight + Max Size.
8888
///
89-
class func font(forStyle style: UIFont.TextStyle, weight: UIFont.Weight) -> UIFont {
89+
class func font(forStyle style: UIFont.TextStyle, weight: UIFont.Weight, maxFontSize: CGFloat? = nil) -> UIFont {
9090
let descriptor = UIFontDescriptor
9191
.preferredFontDescriptor(withTextStyle: style)
9292
.addingAttributes([.traits: [UIFontDescriptor.TraitKey.weight: weight]])
93+
94+
// Limit the font size if needed
95+
if let maxFontSize, descriptor.pointSize > maxFontSize {
96+
return UIFont(descriptor: descriptor, size: maxFontSize)
97+
}
98+
99+
// Return font without a predefined size.
93100
return UIFont(descriptor: descriptor, size: 0)
94101
}
95102

WooCommerce/Classes/Styles/Style.swift

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,20 @@ final class StyleManager {
1111
}
1212

1313
static var statsFont: UIFont {
14-
return self.fontForTextStyle(.title3, weight: .semibold, maximumPointSize: 36.0)
14+
return .font(forStyle: .title3, weight: .semibold, maxFontSize: 36.0)
1515
}
1616

1717
static var statsTitleFont: UIFont {
18-
return self.fontForTextStyle(.caption2, weight: .regular, maximumPointSize: maxFontSize)
18+
return .font(forStyle: .caption2, weight: .regular, maxFontSize: maxFontSize)
1919
}
2020

2121
static var chartLabelFont: UIFont {
2222
// Dashboard chart needs from a slighly smaller maximum font to be able to fit it when using the biggest accessibility font.
23-
return self.fontForTextStyle(.caption2, weight: .regular, maximumPointSize: 20.0)
23+
return .font(forStyle: .caption2, weight: .regular, maxFontSize: 20.0)
2424
}
2525

2626
static var headlineSemiBold: UIFont {
27-
return self.fontForTextStyle(.headline,
28-
weight: .semibold,
29-
maximumPointSize: maxFontSize)
27+
return .font(forStyle: .headline, weight: .semibold, maxFontSize: maxFontSize)
3028
}
3129

3230
static var subheadlineFont: UIFont {
@@ -38,21 +36,15 @@ final class StyleManager {
3836
}
3937

4038
static var subheadlineBoldFont: UIFont {
41-
return self.fontForTextStyle(.subheadline,
42-
weight: .bold,
43-
maximumPointSize: maxFontSize)
39+
return .font(forStyle: .subheadline, weight: .bold, maxFontSize: maxFontSize)
4440
}
4541

4642
static var thinCaptionFont: UIFont {
47-
return self.fontForTextStyle(.caption1,
48-
weight: .thin,
49-
maximumPointSize: maxFontSize)
43+
return .font(forStyle: .caption1, weight: .thin, maxFontSize: maxFontSize)
5044
}
5145

5246
static var footerLabelFont: UIFont {
53-
return self.fontForTextStyle(.footnote,
54-
weight: .regular,
55-
maximumPointSize: maxFontSize)
47+
return .font(forStyle: .footnote, weight: .regular, maxFontSize: maxFontSize)
5648

5749
}
5850

@@ -66,23 +58,3 @@ final class StyleManager {
6658
return UIStatusBarStyle.lightContent
6759
}
6860
}
69-
70-
71-
// MARK: - Private convenience methods
72-
private extension StyleManager {
73-
class func fontForTextStyle(_ style: UIFont.TextStyle, weight: UIFont.Weight, maximumPointSize: CGFloat = maxFontSize) -> UIFont {
74-
let traits = [UIFontDescriptor.TraitKey.weight: weight]
75-
var fontDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: style)
76-
fontDescriptor = fontDescriptor.addingAttributes([.traits: traits])
77-
let fontToGetSize = UIFont(descriptor: fontDescriptor, size: CGFloat(0.0))
78-
return UIFontMetrics(forTextStyle: style).scaledFont(for: fontToGetSize, maximumPointSize: maximumPointSize)
79-
}
80-
81-
82-
private class func fontDescriptor(_ style: UIFont.TextStyle, maximumPointSize: CGFloat = maxFontSize) -> UIFontDescriptor {
83-
let fontDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: style)
84-
let fontToGetSize = UIFont(descriptor: fontDescriptor, size: CGFloat(0.0))
85-
let scaledFontSize = CGFloat.minimum(fontToGetSize.pointSize, maximumPointSize)
86-
return fontDescriptor.withSize(scaledFontSize)
87-
}
88-
}

0 commit comments

Comments
 (0)