Custom fonts on SwiftUI in visionOS for RealityKit app π₯½
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 for using custom fonts in SwiftUI and example reference projects from to no avail.
Later, I came across this Apple Developer Forums post that gave me some hints.
What I realized was that despite ensuring that:
- the font was included in the bundle
- was included in the info.plist with the correct relative path
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: