Tag Archives: tips

UIFont not responding or nil

My font does not respond and it says nil when I print it? What’s happening here?
Actually, if your UIFont happens to be nil, it’s maybe because you’re not calling it properly! Haha, very funny, I know, but let’s have a look at happened to me recently.

Here is what I did:
I imported "MyFont-Regular.ttf" and "MyFont-bold.ttf" in Xcode.
I set the constants with the font name.

And I set the font to that label in the view:

and … nothing happened, well, the label was displayed but not as wanted. I expected something like label2 and label1 happened. And Label3 worked just fine btw…

UIFont Labels example
Label 1, 2 and 3… fail, expected and success

I printed the UIFont and it failed on that line! So I double checked if my fonts were loaded correctly. And they were, so the problem was somewhere else.

Then I remembered I saw something about printing the available fonts in this article: Custom Fonts in Swift.

Those few lines can save you a lot of time and trouble:

After running the little PrintMyFonts() function, I realized that the font I needed was called "MyFont" by the system and not "MyFont-Regular". Aha!

Facepalm

That sounds maybe trivial, but I hope this will help you avoiding headaches and pants-over-head frustration. And remember: do not assume the font name based on the file name, even if it works sometimes ;)

Bonus: the list of default fonts supported on iOS: iOSFonts.com. It lists every font for every iOS version, for iPhone, iPad and Watch, pretty neat!