Skip to content

Commit 6e33030

Browse files
committed
Updated ScaledFont
- now a Swift Package - added a SwiftUI example
1 parent 828445f commit 6e33030

File tree

28 files changed

+927
-423
lines changed

28 files changed

+927
-423
lines changed

ScaledFont/ScaledFont/Noteworthy.plist renamed to ScaledFont/Fonts/Noteworthy.plist

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,77 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5-
<key>UICTFontTextStyleTitle0</key>
5+
<key>largeTitle</key>
66
<dict>
77
<key>fontName</key>
88
<string>Noteworthy-Light</string>
99
<key>fontSize</key>
1010
<integer>34</integer>
1111
</dict>
12-
<key>UICTFontTextStyleTitle1</key>
12+
<key>title</key>
1313
<dict>
1414
<key>fontName</key>
1515
<string>Noteworthy-Light</string>
1616
<key>fontSize</key>
1717
<integer>28</integer>
1818
</dict>
19-
<key>UICTFontTextStyleTitle2</key>
19+
<key>title2</key>
2020
<dict>
2121
<key>fontName</key>
2222
<string>Noteworthy-Light</string>
2323
<key>fontSize</key>
2424
<integer>22</integer>
2525
</dict>
26-
<key>UICTFontTextStyleTitle3</key>
26+
<key>title3</key>
2727
<dict>
2828
<key>fontName</key>
2929
<string>Noteworthy-Light</string>
3030
<key>fontSize</key>
3131
<integer>20</integer>
3232
</dict>
33-
<key>UICTFontTextStyleHeadline</key>
33+
<key>headline</key>
3434
<dict>
3535
<key>fontName</key>
3636
<string>Noteworthy-Bold</string>
3737
<key>fontSize</key>
3838
<integer>17</integer>
3939
</dict>
40-
<key>UICTFontTextStyleSubhead</key>
40+
<key>subheadline</key>
4141
<dict>
4242
<key>fontName</key>
4343
<string>Noteworthy-Light</string>
4444
<key>fontSize</key>
4545
<integer>15</integer>
4646
</dict>
47-
<key>UICTFontTextStyleBody</key>
47+
<key>body</key>
4848
<dict>
4949
<key>fontName</key>
5050
<string>Noteworthy-Light</string>
5151
<key>fontSize</key>
5252
<integer>17</integer>
5353
</dict>
54-
<key>UICTFontTextStyleCallout</key>
54+
<key>callout</key>
5555
<dict>
5656
<key>fontName</key>
5757
<string>Noteworthy-Light</string>
5858
<key>fontSize</key>
5959
<integer>16</integer>
6060
</dict>
61-
<key>UICTFontTextStyleFootnote</key>
61+
<key>footnote</key>
6262
<dict>
6363
<key>fontName</key>
6464
<string>Noteworthy-Light</string>
6565
<key>fontSize</key>
6666
<integer>13</integer>
6767
</dict>
68-
<key>UICTFontTextStyleCaption1</key>
68+
<key>caption</key>
6969
<dict>
7070
<key>fontName</key>
7171
<string>Noteworthy-Light</string>
7272
<key>fontSize</key>
7373
<integer>12</integer>
7474
</dict>
75-
<key>UICTFontTextStyleCaption2</key>
75+
<key>caption2</key>
7676
<dict>
7777
<key>fontName</key>
7878
<string>Noteworthy-Light</string>

ScaledFont/ScaledFont/NotoSerif.plist renamed to ScaledFont/Fonts/NotoSerif.plist

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,77 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5-
<key>UICTFontTextStyleTitle0</key>
5+
<key>largeTitle</key>
66
<dict>
77
<key>fontName</key>
88
<string>NotoSerif</string>
99
<key>fontSize</key>
1010
<integer>34</integer>
1111
</dict>
12-
<key>UICTFontTextStyleTitle1</key>
12+
<key>title</key>
1313
<dict>
1414
<key>fontName</key>
1515
<string>NotoSerif</string>
1616
<key>fontSize</key>
1717
<integer>28</integer>
1818
</dict>
19-
<key>UICTFontTextStyleTitle2</key>
19+
<key>title2</key>
2020
<dict>
2121
<key>fontName</key>
2222
<string>NotoSerif</string>
2323
<key>fontSize</key>
2424
<integer>22</integer>
2525
</dict>
26-
<key>UICTFontTextStyleTitle3</key>
26+
<key>title3</key>
2727
<dict>
2828
<key>fontName</key>
2929
<string>NotoSerif</string>
3030
<key>fontSize</key>
3131
<integer>20</integer>
3232
</dict>
33-
<key>UICTFontTextStyleHeadline</key>
33+
<key>headline</key>
3434
<dict>
3535
<key>fontName</key>
3636
<string>NotoSerif-Bold</string>
3737
<key>fontSize</key>
3838
<integer>17</integer>
3939
</dict>
40-
<key>UICTFontTextStyleSubhead</key>
40+
<key>subheadline</key>
4141
<dict>
4242
<key>fontName</key>
4343
<string>NotoSerif-Italic</string>
4444
<key>fontSize</key>
4545
<integer>15</integer>
4646
</dict>
47-
<key>UICTFontTextStyleBody</key>
47+
<key>body</key>
4848
<dict>
4949
<key>fontName</key>
5050
<string>NotoSerif</string>
5151
<key>fontSize</key>
5252
<integer>17</integer>
5353
</dict>
54-
<key>UICTFontTextStyleCallout</key>
54+
<key>callout</key>
5555
<dict>
5656
<key>fontName</key>
5757
<string>NotoSerif</string>
5858
<key>fontSize</key>
5959
<integer>16</integer>
6060
</dict>
61-
<key>UICTFontTextStyleFootnote</key>
61+
<key>footnote</key>
6262
<dict>
6363
<key>fontName</key>
6464
<string>NotoSerif</string>
6565
<key>fontSize</key>
6666
<integer>13</integer>
6767
</dict>
68-
<key>UICTFontTextStyleCaption1</key>
68+
<key>caption</key>
6969
<dict>
7070
<key>fontName</key>
7171
<string>NotoSerif-Italic</string>
7272
<key>fontSize</key>
7373
<integer>12</integer>
7474
</dict>
75-
<key>UICTFontTextStyleCaption2</key>
75+
<key>caption2</key>
7676
<dict>
7777
<key>fontName</key>
7878
<string>NotoSerif-Italic</string>

ScaledFont/README.md

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,85 @@
22

33
## Using Dynamic Type With Custom Fonts
44

5-
An example of using custom fonts with dynamic type making use of the `UIFontMetrics` class introduced with iOS 11. It provides examples using the Noteworthy font which is built-in to iOS and the Noto Serif font which was downloaded from google fonts:
5+
An example of using custom fonts with dynamic type making use of the `UIFontMetrics` class introduced with iOS 11. It provides examples using the Noteworthy font that Apple ships built-in to iOS and the Noto Serif font which you can download from google fonts:
66

77
+ [Noto Serif](https://fonts.google.com/specimen/Noto+Serif?selection.family=Noto+Serif)
88

9-
*Refer to LICENSE.txt if you plan on using Noto Serif in a shipping application.*
9+
*See the LICENSE.txt file if you plan on using Noto Serif in a shipping application.*
1010

11-
**This project requires iOS 11**
11+
## Sample Targets
1212

13-
### ScaledFont
13+
There are two targets in this project:
1414

15-
A utility class to help you use custom fonts with dynamic type.
15+
+ Sampler - A UIKit example that requires a minimum of iOS 11
16+
+ SamplerUI - A SwiftUI example that requires a minimum of iOS 14
1617

17-
To use this class you must supply the name of a style dictionary for the font when creating the class. The style dictionary should be stored as a property list file in the main bundle.
18+
## Swift Package
1819

19-
The style dictionary contains an entry for each text style that uses the raw string value for each `UIFontTextStyle` as the key.
20+
The ScaledFont implementation is now provided as a Swift Package Manager dependency. You can find the GitHub repository for the package here:
21+
22+
+ [ScaledFont on GitHub](https://github.com/kharrison/ScaledFont)
23+
24+
## ScaledFont Details
25+
26+
The `ScaledFont` type is a utility type to help you use custom fonts with dynamic type. You must give the name of a style dictionary for the font when creating the `ScaledFont`. You include the style dictionary as a property list file in the main bundle of your app.
27+
28+
The style dictionary contains an entry for each text style. The available text styles are:
29+
30+
+ `largeTitle`, `title`, `title2`, `title3`
31+
+ `headline`, `subheadline`, `body`, `callout`
32+
+ `footnote`, `caption`, `caption2`
2033

2134
The value of each entry is a dictionary with two keys:
2235

23-
+ fontName: A String which is the font name.
24-
+ fontSize: A number which is the point size to use at the `.large` content size.
36+
+ `fontName`: A `String` which is the font name.
37+
+ `fontSize`: A number which is the point size to use at the `.large` (base) content size.
2538

2639
For example to use a 17 pt Noteworthy-Bold font for the `.headline` style at the `.large` content size:
2740

28-
<dict>
29-
<key>UICTFontTextStyleHeadline</key>
30-
<dict>
31-
<key>fontName</key>
32-
<string>Noteworthy-Bold</string>
33-
<key>fontSize</key>
34-
<integer>17</integer>
35-
</dict>
36-
</dict>
41+
```
42+
<dict>
43+
<key>headline</key>
44+
<dict>
45+
<key>fontName</key>
46+
<string>Noteworthy-Bold</string>
47+
<key>fontSize</key>
48+
<integer>17</integer>
49+
</dict>
50+
</dict>
51+
```
3752

3853
You do not need to include an entry for every text style but if you try to use a text style that is not included in the dictionary it will fallback to the system preferred font.
3954

40-
### Further Details
55+
## Using With UIKit
56+
57+
For `UIKit`, apply the scaled font to text labels, text fields or text views:
58+
59+
```swift
60+
let scaledFont = ScaledFont(fontName: "Noteworthy")
61+
label.font = scaledFont.font(forTextStyle: .headline)
62+
label.adjustsFontForContentSizeCategory = true
63+
```
64+
65+
Remember to set the `adjustsFontFotContentSizeCategory` property to have the font size adjust automatically when the user changes their preferred content size.
66+
67+
## Using With SwiftUI
68+
69+
For `SwiftUI`, add the scaled font to the environment of a view:
70+
71+
```swift
72+
ContentView()
73+
.environment(\.scaledFont, scaledFont)
74+
```
75+
76+
Then apply the scaled fint view modifier to any view containing text in the view hierarchy:
77+
78+
```swift
79+
Text("Headline")
80+
.scaledFont(.headline)
81+
```
82+
## Further Details
4183

42-
See the following blog post for further details:
84+
See this blog post for further details:
4385

4486
+ [Using A Custom Font With Dynamic Type](https://useyourloaf.com/blog/using-a-custom-font-with-dynamic-type/)

ScaledFont/ScaledFont/AppDelegate.swift renamed to ScaledFont/Sampler/AppDelegate.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// Created by Keith Harrison https://useyourloaf.com
2-
// Copyright (c) 2017 Keith Harrison. All rights reserved.
1+
// Copyright © 2021 Keith Harrison. All rights reserved.
32
//
43
// Redistribution and use in source and binary forms, with or without
54
// modification, are permitted provided that the following conditions are met:
@@ -29,7 +28,6 @@
2928

3029
import UIKit
3130

32-
@UIApplicationMain
33-
class AppDelegate: UIResponder, UIApplicationDelegate {
34-
var window: UIWindow?
31+
@main
32+
final class AppDelegate: UIResponder, UIApplicationDelegate {
3533
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"colors" : [
3+
{
4+
"idiom" : "universal"
5+
}
6+
],
7+
"info" : {
8+
"author" : "xcode",
9+
"version" : 1
10+
}
11+
}

0 commit comments

Comments
 (0)