These are the steps to add a custom font to you application:
- Add "gameOver.ttf" font in your application ( Make sure that it's included in the target)
- Modify the application-info.plist file.
- Add the key "Fonts provided by application" in a new row
- and add "gameOver.ttf" as new item in the Array "Fonts provided by application".
Now the font will be available in Interface Builder.
To use the custom font in code we need to refer to it by name, but the name often isn’t the same as the font’s filename
There are 2 ways to find the name:
- Install the font on your Mac. Open Font Book, open the font and see
what name is listed.
- Programmatically list the available fonts in your app
for the second approach add this line is your app delegate’s didFinishLaunchingWithOptions
print(UIFont.familyNames)
To list the fonts included in each font family, in Swift 5:
func printFonts() {
for familyName in UIFont.familyNames {
print("
-- (familyName)
")
for fontName in UIFont.fontNames(forFamilyName: familyName) {
print(fontName)
}
}
}
after finding the name of your custom font you may use it like this:
SKLabelNode(fontNamed: "gameOver") // put here the correct font name
or in a simple label :
cell.textLabel?.font = UIFont(name: "gameOver", size: 16) // put here the correct font name
Useful resource
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…