載入第三方字型
獲取當前系統支援字型冊
UIFont.familyNames.forEach { (familyName) in
print(familyName)
UIFont.fontNames(forFamilyName: familyName).forEach { (item) in
print(" - " + item)
}
}
複製程式碼
使用指定字型
let font = UIFont(name: "PingFangSC-Regular", size: 18)
複製程式碼
靜態載入
該方式簡單直接,只需在工程中包含字型檔案,並在info.plist宣告便可使用.
-
新增字型檔案
-
info.plist修改
- 增加
Fonts provided by application
欄位 - 在該欄位下新增引入的字型檔名
- 增加
動態載入
通過指定字型檔案路徑載入,該種方式可配合 網路下發/字型加密 使用.
/// 載入本地字型檔案
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 刪除後重灌時是無需再額外下載這個字型的。
-
判斷字型是否存在
if let aFont = UIFont(name: fontName, size: 12), aFont.fontName == fontName || aFont.familyName == fontName{ // 存在 } 複製程式碼
-
下載(具體程式碼請參照swift Demo)
let attrs = [kCTFontNameAttribute:fontName] let desc = CTFontDescriptorCreateWithAttributes(attrs as CFDictionary) let descs = [desc] CTFontDescriptorMatchFontDescriptorsWithProgressHandler(descs as CFArray, nil) { (state, progressParameter) -> Bool in // 下載 } 複製程式碼
注意點
-
iOS 9 以下 系統 預設沒有
PingFang
系列字型. -
使用
UIFont
時name
使用字型postScriptName
.(有些字型檔案與真實名稱不符),可以使用字型冊檢視具體名稱.
附錄:
- iOS 11系統提供字型
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
複製程式碼