Custom fonts on SwiftUI in visionOS for RealityKit app πŸ₯½

β€’visionOS

I'm writing this to save someone some trouble, because I spent way too long debugging custom fonts on Xcode 15.3 and visionOS 1.1 & 1.2.

I first went through the relevant Xcode docs (opens in a new tab) for using custom fonts in SwiftUI and example reference projects (opens in a new tab) from to no avail.

Later, I came across this Apple Developer Forums post (opens in a new tab) that gave me some hints.

What I realized was that despite ensuring that:

the name of the font file != the font name that you can load the Font() with.

For example, if you print this out:

print("Fonts: \(UIFont.familyNames)")

you might see an empty list, which confirms that you're not referencing a font.

How do you find out what the font name is?

Add this print somewhere in your app to see the font names available to you:

print("Fonts: \(UIFont.familyNames)")

You should see a long list, but among them, you should see the font name of what you're trying to load.

Sometimes it has a weird name like 04b_19. Try loading that font name with

Text("Some text").font(.custom("04b_19", size: 60))

and see if it works.

If you're curious, I was trying to load a custom Flappy Bird font for something I was prototyping:


Last Updated: Fri Apr 26 2024

Related

Post

Vision Pro - A developer's experience

March 19 2024

Looking for more posts?


Monthly Newsletter

Coming soon - be the first to receive my new monthly newsletter.


Looking for a full-stack iOS/visionOS/Next.js developer for your project? Email me at hello@skyporch.co.
Β© Gabriel Uribe.RSS