简体   繁体   中英

Swift Custom Fonts Xcode

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
    node.addChild(label)

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

print(UIFont.familyNames)

To list the fonts included in each font family, in Swift 5:

 func printFonts() {
    for familyName in UIFont.familyNames {
        print("\n-- \(familyName) \n")
        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

Swift 4 and 5. I have created an enum for App fonts. First install fonts on system by double click on desired font. Then installed font will appear under Custom fonts in Attribute inspector.

在此处输入图片说明

import Foundation
import UIKit

private let familyName = "Montserrat"

enum AppFont: String {
    case light = "Light"
    case regular = "Regular"
    case bold = "Bold"

    func size(_ size: CGFloat) -> UIFont {
        if let font = UIFont(name: fullFontName, size: size + 1.0) {
            return font
        }
        fatalError("Font '\(fullFontName)' does not exist.")
    }
    fileprivate var fullFontName: String {
        return rawValue.isEmpty ? familyName : familyName + "-" + rawValue
    }
}

Usage

self.titleLabel?.font = AppFont.regular.size(12.0)

Along with the above answer, I would like to add an another answer on installing the custom fonts to Xcode which can be access to Mac as well as MAC.

1. Select and download your favourite font styles from here

2. Unzip the download folder and select all the fonts from folder and double tap to open

3. Pop up appears to select all the fonts > check all and install

4. Open Xcode, one can see from Attribute inspector for selected label

That's all :)

Watch out the attached pic for more ref:

在此处输入图片说明

Font Lato appeared in Xcode workspace

在此处输入图片说明

Just in case you need to add it to your app, also you have to close XCode and open it again so newly added font can show up on the fonts dropdown after adding them to your info.plist.

Check here on their official documentation: https://developer.apple.com/documentation/uikit/text_display_and_fonts/adding_a_custom_font_to_your_app

What solved the problem for me:

Even if you place the.ttf font file within a subdirectory, in the Info.plist file, make sure to refer to the file as just "filename.ttf." Don't add the subdirectory name in the filepath, such as "subdirectory/filename.ttf"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM