Skip to content

Commit 3f2bd1e

Browse files
billzhou0223shengxu7
authored andcommitted
fix: πŸ› map fonts with unsupported weight and trait combo to existing 72 fonts. (#536)
* fix: πŸ› map unsupported weight and trait combo to existing fonts * chore: πŸ€– Fix a minor compilation issue * chore: πŸ€– fix a compilation issue * chore: πŸ€– fix a compilation issue * chore: πŸ€– fix a compilation issue
1 parent 3df1ea6 commit 3f2bd1e

File tree

2 files changed

+111
-37
lines changed

2 files changed

+111
-37
lines changed

Diff for: β€ŽApps/Examples/Examples/FioriThemeManager/72-Fonts.swift

+1-13
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct FioriFonts: View {
1515
ForEach(0 ..< textStyles.count) { index in
1616
let textStyle = textStyles[index]
1717
Text(String(describing: textStyle))
18-
.font(fioriFont(textStyle))
18+
.font(.fiori(forTextStyle: textStyle, weight: fontWeight, isItalic: isItalic, isCondensed: isCondensed))
1919
}
2020
}
2121
.environment(\.sizeCategory, sizeCategory)
@@ -32,18 +32,6 @@ struct FioriFonts: View {
3232
Settings(fontWeight: $fontWeight, isItalic: $isItalic, isCondensed: $isCondensed, sizeCategory: $sizeCategory)
3333
}
3434
}
35-
36-
func fioriFont(_ textStyle: Font.FioriTextStyle) -> Font {
37-
var font: Font = self.isCondensed ? .fioriCondensed(forTextStyle: textStyle) : .fiori(forTextStyle: textStyle)
38-
39-
font = font.weight(self.fontWeight)
40-
41-
if self.isItalic {
42-
font = font.italic()
43-
}
44-
45-
return font
46-
}
4735
}
4836

4937
extension FioriFonts {

Diff for: β€ŽSources/FioriThemeManager/72-Fonts/Font+Fiori.swift

+110-24
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,30 @@ public extension Font {
99
///
1010
/// - Parameter fioriTextStyle: Text style.
1111
/// - Returns: A scaled font for this text style.
12-
static func fiori(forTextStyle fioriTextStyle: Font.FioriTextStyle) -> Font {
13-
guard UIFont.familyNames.contains("72") else {
14-
return Font.system(fioriTextStyle.textStyle)
12+
static func fiori(forTextStyle fioriTextStyle: Font.FioriTextStyle, weight: Font.Weight = .regular, isItalic: Bool = false, isCondensed: Bool = false) -> Font {
13+
var font: Font
14+
if UIFont.familyNames.contains("72") {
15+
font = Font.custom("72", size: fioriTextStyle.size, relativeTo: fioriTextStyle.textStyle)
16+
font = font.weight(weight.getFioriWeight(isItalic: isItalic, isCondensed: isCondensed))
17+
} else {
18+
font = Font.system(fioriTextStyle.textStyle).weight(weight)
1519
}
1620

17-
if #available(iOS 14.0, *) {
18-
return Font.custom("72", size: fioriTextStyle.size, relativeTo: fioriTextStyle.textStyle)
19-
} else {
20-
return Font.custom("72", size: fioriTextStyle.size)
21+
if isItalic {
22+
font = font.italic()
23+
} else if isCondensed {
24+
#if swift(>=5.7.1)
25+
if #available(iOS 16.0, watchOS 9.0, *) {
26+
font = font.width(Font.Width.condensed)
27+
} else {
28+
font = Font.fioriCondensed(forTextStyle: fioriTextStyle, weight: weight)
29+
}
30+
#else
31+
font = Font.fioriCondensed(forTextStyle: fioriTextStyle, weight: weight)
32+
#endif
2133
}
34+
35+
return font
2236
}
2337

2438
/// Fiori (72) fonts
@@ -27,13 +41,30 @@ public extension Font {
2741
///
2842
/// - Parameter fioriTextStyle: Text style.
2943
/// - Returns: A font with fixed size.
30-
@available(iOS 14.0, *)
31-
static func fiori(fixedSize: CGFloat) -> Font {
32-
guard UIFont.familyNames.contains("72") else {
33-
return Font.system(size: fixedSize)
44+
static func fiori(fixedSize: CGFloat, weight: Font.Weight = .regular, isItalic: Bool = false, isCondensed: Bool = false) -> Font {
45+
var font: Font
46+
if UIFont.familyNames.contains("72") {
47+
font = Font.custom("72", fixedSize: fixedSize)
48+
font = font.weight(weight.getFioriWeight(isItalic: isItalic, isCondensed: isCondensed))
49+
} else {
50+
font = Font.system(size: fixedSize).weight(weight)
3451
}
3552

36-
return Font.custom("72", fixedSize: fixedSize)
53+
if isItalic {
54+
font = font.italic()
55+
} else if isCondensed {
56+
#if swift(>=5.7.1)
57+
if #available(iOS 16.0, watchOS 9.0, *) {
58+
font = font.width(Font.Width.condensed)
59+
} else {
60+
font = Font.fioriCondensed(fixedSize: fixedSize, weight: weight)
61+
}
62+
#else
63+
font = Font.fioriCondensed(fixedSize: fixedSize, weight: weight)
64+
#endif
65+
}
66+
67+
return font
3768
}
3869

3970
/// Fiori (72) condensed fonts
@@ -42,16 +73,22 @@ public extension Font {
4273
///
4374
/// - Parameter fioriTextStyle: Text style.
4475
/// - Returns: A scaled condensed font for this text style.
45-
static func fioriCondensed(forTextStyle fioriTextStyle: Font.FioriTextStyle) -> Font {
46-
guard UIFont.familyNames.contains("72") else {
47-
return Font.system(fioriTextStyle.textStyle)
48-
}
49-
50-
if #available(iOS 14.0, *) {
51-
return Font.custom("72-Condensed", size: fioriTextStyle.size, relativeTo: fioriTextStyle.textStyle)
76+
@available(*, deprecated, message: "Use UIFont.fiori(forTextStyle:) with isCondensed parameter set to true")
77+
static func fioriCondensed(forTextStyle fioriTextStyle: Font.FioriTextStyle, weight: Font.Weight = .regular) -> Font {
78+
var font: Font
79+
if UIFont.familyNames.contains("72") {
80+
font = Font.custom("72-Condensed", size: fioriTextStyle.size, relativeTo: fioriTextStyle.textStyle)
81+
font = font.weight(weight.getFioriWeight(isItalic: false, isCondensed: true))
5282
} else {
53-
return Font.custom("72-Condensed", size: fioriTextStyle.size)
83+
font = Font.system(fioriTextStyle.textStyle).weight(weight)
84+
#if swift(>=5.7.1)
85+
if #available(iOS 16.0, watchOS 9.0, *) {
86+
font = font.width(Font.Width.condensed)
87+
}
88+
#endif
5489
}
90+
91+
return font
5592
}
5693

5794
/// Fiori (72) condensed fonts
@@ -61,12 +98,22 @@ public extension Font {
6198
/// - Parameter fioriTextStyle: Text style.
6299
/// - Returns: A condensed font with fixed size.
63100
@available(iOS 14.0, *)
64-
static func fioriCondensed(fixedSize: CGFloat) -> Font {
65-
guard UIFont.familyNames.contains("72") else {
66-
return Font.system(size: fixedSize)
101+
@available(*, deprecated, message: "Use UIFont.fiori(fixedSize:) with isCondensed parameter set to true")
102+
static func fioriCondensed(fixedSize: CGFloat, weight: Font.Weight = .regular) -> Font {
103+
var font: Font
104+
if UIFont.familyNames.contains("72") {
105+
font = Font.custom("72-Condensed", fixedSize: fixedSize)
106+
font = font.weight(weight.getFioriWeight(isItalic: false, isCondensed: true))
107+
} else {
108+
font = Font.system(size: fixedSize).weight(weight)
109+
#if swift(>=5.7.1)
110+
if #available(iOS 16.0, watchOS 9.0, *) {
111+
font = font.width(Font.Width.condensed)
112+
}
113+
#endif
67114
}
68115

69-
return Font.custom("72-Condensed", fixedSize: fixedSize)
116+
return font
70117
}
71118
}
72119

@@ -188,3 +235,42 @@ extension Font.FioriTextStyle: CustomStringConvertible {
188235
}
189236
}
190237
}
238+
239+
extension Font.Weight {
240+
func getFioriWeight(isItalic: Bool, isCondensed: Bool) -> Font.Weight {
241+
isItalic ? self.italicWeight : (isCondensed ? self.condensedWeight : self.fioriWeight)
242+
}
243+
244+
private var fioriWeight: Font.Weight {
245+
switch self {
246+
case .heavy, .black:
247+
return .black
248+
case .medium, .semibold, .bold:
249+
return .bold
250+
case .regular:
251+
return .regular
252+
case .ultraLight, .thin, .light:
253+
return .light
254+
default:
255+
return .regular
256+
}
257+
}
258+
259+
private var italicWeight: Font.Weight {
260+
switch self.fioriWeight {
261+
case .black, .bold:
262+
return .bold
263+
default:
264+
return .regular
265+
}
266+
}
267+
268+
private var condensedWeight: Font.Weight {
269+
switch self.fioriWeight {
270+
case .black, .bold:
271+
return .bold
272+
default:
273+
return .regular
274+
}
275+
}
276+
}

0 commit comments

Comments
Β (0)