Swift Custom Fonts Xcode

Swift Custom Fonts Xcode

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

Adding custom font to Xcode 13+

  1. drag and drop your font-file into the project (make sure you hit copy if needed and check the target box)

drag&drop


  1. choose Project -> Targets -> Info -> Custom iOS Target Properties and klick the small + icon under the "key" property. Start typing "Fonts provided by application"

Sample Image


  1. enter the full font-filename inside this property-list

Sample Image


  1. now you can use the font by its name

  2. (Bonus: if you have trouble to find the font name, you can print all available fonts with [see detail explanation for this at code with chris ]:

 for family: String in UIFont.familyNames
{
print(family)
for names: String in UIFont.fontNames(forFamilyName: family)
{
print("== \(names)")
}
}

How to change font in Xcode Swift Playgrounds(.swiftpm) project?

I found the solution by myself with the help of a friend, I had to create a public struct MyFont with a fileprivate static func, and then add an init inside the main file, before var body.

This is the code:

MyFont

import SwiftUI

public struct MyFont {
public static func registerFonts() {
registerFont(bundle: Bundle.main , fontName: "YOUR-FONT-HERE", fontExtension: ".ttf") //change according to your ext.
}
fileprivate static func registerFont(bundle: Bundle, fontName: String, fontExtension: String) {

guard let fontURL = bundle.url(forResource: fontName, withExtension: fontExtension),
let fontDataProvider = CGDataProvider(url: fontURL as CFURL),
let font = CGFont(fontDataProvider) else {
fatalError("Couldn't create font from data")
}

var error: Unmanaged<CFError>?

CTFontManagerRegisterGraphicsFont(font, &error)
}
}

MyApp

@main
struct MyApp: App {

//add the init before var body
init() {
MyFont.registerFonts()
}

var body: some Scene {
//
}
}

Custom Fonts not showing on Xcode 11 (macOS Catalina)

Unfortunately, the only solution for me was a fresh install of mac os catalina. Now everything works correctly in xCode interface builder.

iOS Custom font without adding to system

A storyboard does not magically look in your project for fonts. So opening a storyboard will not display a font unless that font is available on your machine as a whole.

The usual approach is to set the font in code, not in the storyboard.

Use custom font in swift

Fist make sure that your font is .ttf or .otf format

  1. Import your font into the project
  2. Add new key "Fonts provided by application" on application's info.plist file and add your font names
    Now you can use custom fonts with interface builder or programatically

Sample Image

yourLabel.font = UIFont.init(name: YourFont, size: size)

Custom font not loading in SwiftUI project after moving code to another project?

Although it looks like you have already done this, just go through the following checklist once:

  1. Fonts (.ttf files) are present inside the project
    font files location
  2. Entry in info.plist file
    Sample Image
  3. Entry in "Copy Bundle Resources" under "Build Settings" of your target
    Sample Image
    If this is missing, add by clicking on the plus (+) icon in this section
  4. Make sure you are using the font correctly (check spelling errors?)
var FontRegular : Font = Font.custom("Poppins-Regular", size: 16)
var FontBold : Font = Font.custom("Poppins-Bold", size: 16)

...

Text("Sample Text")
.font(FontRegular)


Related Topics



Leave a reply



Submit