theenigma theenigma - 2 years ago 94
Swift Question

Swift Custom Fonts Xcode 7.0 Beta

I am creating a game in Xcode (Version 7.0 Beta) using Swift, and I would like to display the label "Game Over" at the end of the game in the font "gameOver.ttf". I have added the font to my resources folder. I do not know how to reference it in my code. Can I please get help?
My Code:

let label = SKLabelNode(fontNamed: "gameOver")
label.text = "Game Over"
label.fontColor = SKColor.redColor()
label.fontSize = 150
label.position = CGPointMake(0, 100)
label.horizontalAlignmentMode = .Center

Answer Source

These are the steps to add a custom font to you application:

  1. Add "gameOver.ttf" font in your application ( Make sure that it's included in the target)
  2. Modify the application-info.plist file.
  3. Add the key "Fonts provided by application" in a new row
  4. 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:

  1. Install the font on your Mac. Open Font Book, open the font and see what name is listed.
  2. Programmatically list the available fonts in your app

for the second approach add this line is your app delegate’s didFinishLaunchingWithOptions


To list the fonts included in each font family, in swift 2:

class func printFonts() {
    for familyName in UIFont.familyNames() {
        print("\n-- \(familyName) \n")
        for fontName in UIFont.fontNamesForFamilyName(familyName) {

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download