user13695158
user13695158

Reputation:

Custom Fonts not showing on font family list

I'm using Swift 5 and Xcode 11.4.1 on macOS Catalina.

I have had no problem applying custom font til today. I'm trying to apply custom font to a label called "textLabel". Let me show you how I tried first and then please tell me what is wrong.

So I tried another way, programmatically.

  1. Same process as mentioned above until the number 3.
  2. I wrote this code inside of viewController.swift file.
import UIKit

class ViewController: UIViewController {
    
  @IBOutlet weak var textLabel: UILabel!
   
  override func viewDidLoad() {
    super.viewDidLoad()
        
    textLabel.font = UIFont(name: "myFont.ttf", size: 22)
  }
}
  1. I ran the app, It shows just normal system font.

I tried both ways over and over (even I created new files for 5 times to just test this font thing)and also I tried with various fonts but same thing happened.

Upvotes: 4

Views: 1720

Answers (1)

Frankenstein
Frankenstein

Reputation: 16351

The issue is with the name you're providing for UIFont in the declaration UIFont(name: "myFont.ttf", size: 22). You're not supposed to provide the name myFont instead you're supposed to provide the actual name of the font eg: Avalon. You can get the font by listing out the entire list of fonts you have. Like this:

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

Now check the console output, look for your new font's 'family' and 'font' name. Pass whatever is displayed as the 'font' name corresponding to your new font family (there could be more than one 'font' associated with your new font 'family') to let myNewFont = UIFont(name: "font_name_from_console_output", size: 22) and you should be in business!

Upvotes: 2

Related Questions