DayByDay/Fonts/HeadingFont.swift
54 lines · 2017 bytes
4 symbols in this file
1//
2// HeadingFont.swift
3// DayByDay
4//
5
6import CoreText
7import SwiftUI
8import UIKit
9
10/// Baloo 2 (SIL Open Font License, Baloo2-OFL.txt) for the headings and
11/// labels a child sees. The Grown-Ups screens keep the system font.
12enum HeadingFont {
13 static let bold = "Baloo2-Bold"
14 static let extraBold = "Baloo2-ExtraBold"
15
16 /// Registers the bundled font and styles navigation titles with it, at
17 /// the text size set when the app launches.
18 static func register() {
19 if let url = Bundle.main.url(forResource: "Baloo2", withExtension: "ttf") {
20 CTFontManagerRegisterFontsForURL(url as CFURL, .process, nil)
21 }
22 if let large = UIFont(name: extraBold, size: 34), let inline = UIFont(name: bold, size: 19) {
23 UINavigationBar.appearance().largeTitleTextAttributes =
24 [.font: UIFontMetrics(forTextStyle: .largeTitle).scaledFont(for: large)]
25 UINavigationBar.appearance().titleTextAttributes =
26 [.font: UIFontMetrics(forTextStyle: .headline).scaledFont(for: inline, maximumPointSize: 28)]
27 }
28 }
29
30 /// The text style whose default size is nearest `size`, so a heading
31 /// grows with Dynamic Type at the same rate as system text that size.
32 static func textStyle(for size: CGFloat) -> Font.TextStyle {
33 switch size {
34 case ..<12: .caption2
35 case ..<14: .footnote
36 case ..<16.5: .callout
37 case ..<18.5: .body
38 case ..<21: .title3
39 case ..<25: .title2
40 case ..<31: .title
41 default: .largeTitle
42 }
43 }
44}
45
46extension Font {
47 /// A heading in Baloo 2: ExtraBold for `.heavy` and `.black`, Bold otherwise.
48 /// Scales with Dynamic Type like the text style nearest `size`.
49 static func heading(_ size: CGFloat, weight: Font.Weight = .bold) -> Font {
50 .custom(weight == .heavy || weight == .black ? HeadingFont.extraBold : HeadingFont.bold,
51 size: size, relativeTo: HeadingFont.textStyle(for: size))
52 }
53}
54