user3333917
user3333917

Reputation: 33

Custom Font not working in Swift/Xcode

I have a problem with a custom font in my project. I tried both ways: by code and interface builder.

I added TeamSpiritNF.ttf to the project and added it to the info.plist (Fonts provided by application).

Then in the code I entered this:

self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "TeamSpiritNF", size: 30)!]

When entering the screen in the simulator the App crashes:

fatal error: unexpectedly found nil while unwrapping an Optional value

But the same line works fine if I change the font name to Helvetica for example.

And if I use this font for a label in the interface builder (where I can select it from the dropdown), it is shown correct in the preview. But when I run the application in the simulator entering that screen is delayed and the label uses Helvetica Neue instead of TeamSpiritNF.

Anyone has an idea what the problem could be?

Upvotes: 3

Views: 7005

Answers (2)

Cesare
Cesare

Reputation: 9409

  1. Create a new folder in your project directory and move your custom fonts there.
  2. In the left sidebar, select your target, then Build Phases and you should see Copy Bundle Resources.
  3. Make sure your fonts are included in the list. If they aren't, include them by clicking on the + button.

Upvotes: 15

Mario Jaramillo
Mario Jaramillo

Reputation: 567

  1. Make sure your fonts are in info.plist (Fonts provided by application)
  2. Select yor target, in the build phases tab, Copy Bundle Resources, add your fonts.

Upvotes: 7

Related Questions