iOS-加载第三方字体

加载第三方字体

获取当前系统支持字体册

1
2
3
4
5
6
UIFont.familyNames.forEach { (familyName) in
print(familyName)
UIFont.fontNames(forFamilyName: familyName).forEach { (item) in
print(" - " + item)
}
}

使用指定字体

1
let font = UIFont(name: "PingFangSC-Regular", size: 18)

静态加载

该方式简单直接,只需在工程中包含字体文件,并在info.plist声明便可使用.

  1. 添加字体文件

  2. info.plist修改

    • 增加Fonts provided by application字段
    • 在该字段下添加引入的字体文件名

动态加载

通过指定字体文件路径加载,该种方式可配合 网络下发/字体加密 使用.

1
2
3
4
5
6
7
8
9
 /// 加载本地字体文件
func custom(fileURL: URL, size: CGFloat) -> UIFont? {
guard let dataProvider = CGDataProvider(url: fileURL as CFURL),
let cgFont = CGFont(dataProvider),
/// 注册字体
CTFontManagerRegisterGraphicsFont(cgFont, nil),
let fontName = cgFont.postScriptName as String? else { return nil }
return UIFont(name: fontName, size: size)
}

动态下载苹果提供的多种中文字体

从 iOS 6 开始,苹果提供了一个新的 API 可以让我们动态下载苹果提供的额外字体。并且,这些字体是下载到系统中,也就是说,如果其他 App 也使用了这个字体或者 App 删除后重装时是无需再额外下载这个字体的。

OC 官方Demo / swift Demo

  1. 判断字体是否存在

    1
    2
    3
    4
       if let aFont = UIFont(name: fontName, size: 12),
    aFont.fontName == fontName || aFont.familyName == fontName{
    // 存在
    }
  2. 下载(具体代码请参照swift Demo)

    1
    2
    3
    4
    5
    6
    let attrs = [kCTFontNameAttribute:fontName]
    let desc = CTFontDescriptorCreateWithAttributes(attrs as CFDictionary)
    let descs = [desc]
    CTFontDescriptorMatchFontDescriptorsWithProgressHandler(descs as CFArray, nil) { (state, progressParameter) -> Bool in
    // 下载
    }
注意点
  1. iOS 9 以下 系统 默认没有PingFang系列字体.

  2. 使用UIFontname使用字体postScriptName.(有些字体文件与真实名称不符),可以使用字体册查看具体名称.

附录:

  1. iOS 11系统提供字体
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
Copperplate
- Copperplate-Light
- Copperplate
- Copperplate-Bold
Heiti SC
Apple SD Gothic Neo
- AppleSDGothicNeo-Thin
- AppleSDGothicNeo-Light
- AppleSDGothicNeo-Regular
- AppleSDGothicNeo-Bold
- AppleSDGothicNeo-SemiBold
- AppleSDGothicNeo-UltraLight
- AppleSDGothicNeo-Medium
Thonburi
- Thonburi
- Thonburi-Light
- Thonburi-Bold
Gill Sans
- GillSans-Italic
- GillSans-SemiBold
- GillSans-UltraBold
- GillSans-Light
- GillSans-Bold
- GillSans
- GillSans-SemiBoldItalic
- GillSans-BoldItalic
- GillSans-LightItalic
Marker Felt
- MarkerFelt-Thin
- MarkerFelt-Wide
Hiragino Maru Gothic ProN
- HiraMaruProN-W4
Courier New
- CourierNewPS-ItalicMT
- CourierNewPSMT
- CourierNewPS-BoldItalicMT
- CourierNewPS-BoldMT
Kohinoor Telugu
- KohinoorTelugu-Regular
- KohinoorTelugu-Medium
- KohinoorTelugu-Light
Heiti TC
Avenir Next Condensed
- AvenirNextCondensed-Heavy
- AvenirNextCondensed-MediumItalic
- AvenirNextCondensed-Regular
- AvenirNextCondensed-UltraLightItalic
- AvenirNextCondensed-Medium
- AvenirNextCondensed-HeavyItalic
- AvenirNextCondensed-DemiBoldItalic
- AvenirNextCondensed-Bold
- AvenirNextCondensed-DemiBold
- AvenirNextCondensed-BoldItalic
- AvenirNextCondensed-Italic
- AvenirNextCondensed-UltraLight
Tamil Sangam MN
- TamilSangamMN
- TamilSangamMN-Bold
Helvetica Neue
- HelveticaNeue-UltraLightItalic
- HelveticaNeue-Medium
- HelveticaNeue-MediumItalic
- HelveticaNeue-UltraLight
- HelveticaNeue-Italic
- HelveticaNeue-Light
- HelveticaNeue-ThinItalic
- HelveticaNeue-LightItalic
- HelveticaNeue-Bold
- HelveticaNeue-Thin
- HelveticaNeue-CondensedBlack
- HelveticaNeue
- HelveticaNeue-CondensedBold
- HelveticaNeue-BoldItalic
Gurmukhi MN
- GurmukhiMN-Bold
- GurmukhiMN
Georgia
- Georgia-BoldItalic
- Georgia-Italic
- Georgia
- Georgia-Bold
Times New Roman
- TimesNewRomanPS-ItalicMT
- TimesNewRomanPS-BoldItalicMT
- TimesNewRomanPS-BoldMT
- TimesNewRomanPSMT
Sinhala Sangam MN
- SinhalaSangamMN-Bold
- SinhalaSangamMN
Arial Rounded MT Bold
- ArialRoundedMTBold
Kailasa
- Kailasa-Bold
- Kailasa
Kohinoor Devanagari
- KohinoorDevanagari-Regular
- KohinoorDevanagari-Light
- KohinoorDevanagari-Semibold
Kohinoor Bangla
- KohinoorBangla-Regular
- KohinoorBangla-Semibold
- KohinoorBangla-Light
Chalkboard SE
- ChalkboardSE-Bold
- ChalkboardSE-Light
- ChalkboardSE-Regular
Apple Color Emoji
- AppleColorEmoji
PingFang TC
- PingFangTC-Regular
- PingFangTC-Thin
- PingFangTC-Medium
- PingFangTC-Semibold
- PingFangTC-Light
- PingFangTC-Ultralight
Gujarati Sangam MN
- GujaratiSangamMN
- GujaratiSangamMN-Bold
Geeza Pro
- GeezaPro-Bold
- GeezaPro
Damascus
- DamascusBold
- DamascusLight
- Damascus
- DamascusMedium
- DamascusSemiBold
Noteworthy
- Noteworthy-Bold
- Noteworthy-Light
Avenir
- Avenir-Oblique
- Avenir-HeavyOblique
- Avenir-Heavy
- Avenir-BlackOblique
- Avenir-BookOblique
- Avenir-Roman
- Avenir-Medium
- Avenir-Black
- Avenir-Light
- Avenir-MediumOblique
- Avenir-Book
- Avenir-LightOblique
Mishafi
- DiwanMishafi
Academy Engraved LET
- AcademyEngravedLetPlain
Futura
- Futura-CondensedExtraBold
- Futura-Medium
- Futura-Bold
- Futura-CondensedMedium
- Futura-MediumItalic
Party LET
- PartyLetPlain
Kannada Sangam MN
- KannadaSangamMN-Bold
- KannadaSangamMN
Arial Hebrew
- ArialHebrew-Bold
- ArialHebrew-Light
- ArialHebrew
Farah
- Farah
Arial
- Arial-BoldMT
- Arial-BoldItalicMT
- Arial-ItalicMT
- ArialMT
Chalkduster
- Chalkduster
Kefa
- Kefa-Regular
Hoefler Text
- HoeflerText-Italic
- HoeflerText-Black
- HoeflerText-Regular
- HoeflerText-BlackItalic
Optima
- Optima-ExtraBlack
- Optima-BoldItalic
- Optima-Italic
- Optima-Regular
- Optima-Bold
Palatino
- Palatino-Italic
- Palatino-Roman
- Palatino-BoldItalic
- Palatino-Bold
Malayalam Sangam MN
- MalayalamSangamMN-Bold
- MalayalamSangamMN
Al Nile
- AlNile
- AlNile-Bold
Lao Sangam MN
- LaoSangamMN
Bradley Hand
- BradleyHandITCTT-Bold
Hiragino Mincho ProN
- HiraMinProN-W3
- HiraMinProN-W6
PingFang HK
- PingFangHK-Medium
- PingFangHK-Thin
- PingFangHK-Regular
- PingFangHK-Ultralight
- PingFangHK-Semibold
- PingFangHK-Light
Helvetica
- Helvetica-Oblique
- Helvetica-BoldOblique
- Helvetica
- Helvetica-Light
- Helvetica-Bold
- Helvetica-LightOblique
Courier
- Courier-BoldOblique
- Courier-Oblique
- Courier
- Courier-Bold
Cochin
- Cochin-Italic
- Cochin-Bold
- Cochin
- Cochin-BoldItalic
Trebuchet MS
- TrebuchetMS-Bold
- TrebuchetMS-Italic
- Trebuchet-BoldItalic
- TrebuchetMS
Devanagari Sangam MN
- DevanagariSangamMN
- DevanagariSangamMN-Bold
Oriya Sangam MN
- OriyaSangamMN
- OriyaSangamMN-Bold
Rockwell
- Rockwell-Italic
- Rockwell-Regular
- Rockwell-Bold
- Rockwell-BoldItalic
Snell Roundhand
- SnellRoundhand
- SnellRoundhand-Bold
- SnellRoundhand-Black
Zapf Dingbats
- ZapfDingbatsITC
Bodoni 72
- BodoniSvtyTwoITCTT-Bold
- BodoniSvtyTwoITCTT-BookIta
- BodoniSvtyTwoITCTT-Book
Verdana
- Verdana-Italic
- Verdana
- Verdana-Bold
- Verdana-BoldItalic
American Typewriter
- AmericanTypewriter-CondensedBold
- AmericanTypewriter-Condensed
- AmericanTypewriter-CondensedLight
- AmericanTypewriter
- AmericanTypewriter-Bold
- AmericanTypewriter-Semibold
- AmericanTypewriter-Light
Avenir Next
- AvenirNext-Medium
- AvenirNext-DemiBoldItalic
- AvenirNext-DemiBold
- AvenirNext-HeavyItalic
- AvenirNext-Regular
- AvenirNext-Italic
- AvenirNext-MediumItalic
- AvenirNext-UltraLightItalic
- AvenirNext-BoldItalic
- AvenirNext-Heavy
- AvenirNext-Bold
- AvenirNext-UltraLight
Baskerville
- Baskerville-SemiBoldItalic
- Baskerville-SemiBold
- Baskerville-BoldItalic
- Baskerville
- Baskerville-Bold
- Baskerville-Italic
Khmer Sangam MN
- KhmerSangamMN
Didot
- Didot-Bold
- Didot
- Didot-Italic
Savoye LET
- SavoyeLetPlain
Bodoni Ornaments
- BodoniOrnamentsITCTT
Symbol
- Symbol
Charter
- Charter-BlackItalic
- Charter-Bold
- Charter-Roman
- Charter-Black
- Charter-BoldItalic
- Charter-Italic
Menlo
- Menlo-BoldItalic
- Menlo-Bold
- Menlo-Italic
- Menlo-Regular
Noto Nastaliq Urdu
- NotoNastaliqUrdu
Bodoni 72 Smallcaps
- BodoniSvtyTwoSCITCTT-Book
DIN Alternate
- DINAlternate-Bold
Papyrus
- Papyrus-Condensed
- Papyrus
Hiragino Sans
- HiraginoSans-W3
- HiraginoSans-W6
PingFang SC
- PingFangSC-Medium
- PingFangSC-Semibold
- PingFangSC-Light
- PingFangSC-Ultralight
- PingFangSC-Regular
- PingFangSC-Thin
Myanmar Sangam MN
- MyanmarSangamMN
- MyanmarSangamMN-Bold
Zapfino
- Zapfino
Telugu Sangam MN
Bodoni 72 Oldstyle
- BodoniSvtyTwoOSITCTT-BookIt
- BodoniSvtyTwoOSITCTT-Book
- BodoniSvtyTwoOSITCTT-Bold
Euphemia UCAS
- EuphemiaUCAS
- EuphemiaUCAS-Italic
- EuphemiaUCAS-Bold
Bangla Sangam MN
DIN Condensed
- DINCondensed-Bold