Skip to content

Commit 70cd367

Browse files
Mark Pospeselmpospese
authored andcommitted
Allow user to change Typography’s default font factory
1 parent 8fc6ca4 commit 70cd367

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

Sources/YMatterType/Typography/Typography.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,15 @@ public struct Typography {
3333
public let textStyle: UIFont.TextStyle
3434
/// Whether this font is fixed in size or should be scaled through Dynamic Type
3535
public let isFixed: Bool
36-
36+
37+
/// The factory to use to convert from family name + font style into a `FontFamily`.
38+
/// The default is to use `DefaultFontFamilyFactory`.
39+
///
40+
/// If you use a custom font family (or families) in your project, create your own factory to
41+
/// return the correct font family and then set it here, preferably as early as possible in the
42+
/// app launch lifecycle.
43+
public static var factory: FontFamilyFactory = DefaultFontFamilyFactory()
44+
3745
/// Initializes a typography instance with the specified parameters
3846
/// - Parameters:
3947
/// - fontFamily: font family to use
@@ -102,7 +110,7 @@ public struct Typography {
102110
isFixed: Bool = false
103111
) {
104112
self.init(
105-
fontFamily: DefaultFontFamily(familyName: familyName, style: fontStyle),
113+
fontFamily: Self.factory.getFontFamily(familyName: familyName, style: fontStyle),
106114
fontWeight: fontWeight,
107115
fontSize: fontSize,
108116
lineHeight: lineHeight,

Tests/YMatterTypeTests/Typography/TypogaphyTests.swift

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,58 @@ final class TypogaphyTests: XCTestCase {
1313
func testInit() {
1414
// test the default initializer
1515
let avenir = DefaultFontFamily(familyName: "AvenirNext")
16-
let typeInfo = Typography(fontFamily: avenir, fontWeight: .bold, fontSize: 16, lineHeight: 24)
17-
18-
XCTAssertNotNil(typeInfo.generateLayout(compatibleWith: .default))
16+
let typeInfo = Typography(fontFamily: avenir, fontWeight: .regular, fontSize: 16, lineHeight: 24)
17+
let layout = typeInfo.generateLayout(compatibleWith: .default)
18+
19+
XCTAssertEqual(layout.font.familyName, "Avenir Next")
1920
_testDefaults(typeInfo)
2021
}
2122

2223
func testInit2() {
2324
// test the convenience initializer
24-
let typeInfo = Typography(familyName: "AvenirNext", fontWeight: .bold, fontSize: 16, lineHeight: 24)
25-
26-
XCTAssertNotNil(typeInfo.generateLayout(compatibleWith: .default))
25+
let typeInfo = Typography(familyName: "AvenirNext", fontWeight: .regular, fontSize: 16, lineHeight: 24)
26+
let layout = typeInfo.generateLayout(compatibleWith: .default)
27+
28+
XCTAssertEqual(layout.font.familyName, "Avenir Next")
2729
_testDefaults(typeInfo)
2830
}
2931

32+
func testFactory() throws {
33+
// Given
34+
Typography.factory = NotoSansFactory()
35+
try UIFont.register(name: "NotoSans-Regular")
36+
addTeardownBlock {
37+
Typography.factory = DefaultFontFamilyFactory()
38+
try UIFont.unregister(name: "NotoSans-Regular")
39+
}
40+
41+
// When
42+
let typeInfo = Typography(familyName: "AvenirNext", fontWeight: .regular, fontSize: 16, lineHeight: 24)
43+
let layout = typeInfo.generateLayout(compatibleWith: .default)
44+
45+
// Then
46+
XCTAssertEqual(layout.font.familyName, "Noto Sans")
47+
}
48+
3049
private func _testDefaults(_ typography: Typography) {
3150
// Confirm default init parameter values
3251
XCTAssertEqual(typography.letterSpacing, 0)
3352
XCTAssertEqual(typography.textStyle, UIFont.TextStyle.body)
3453
XCTAssertFalse(typography.isFixed)
3554
}
3655
}
56+
57+
struct NotoSansFontFamily: FontFamily {
58+
let familyName = "NotoSans"
59+
}
60+
61+
extension Typography {
62+
static let notoSans = NotoSansFontFamily()
63+
}
64+
65+
struct NotoSansFactory: FontFamilyFactory {
66+
// Always returns Noto Sans font family
67+
func getFontFamily(familyName: String, style: YMatterType.Typography.FontStyle) -> YMatterType.FontFamily {
68+
Typography.notoSans
69+
}
70+
}

0 commit comments

Comments
 (0)