Installing & Using a Custom Font for iOS App

This has been a learning Journey

I've struggled in understanding some of the Apple ecosystem.  One of the early reasons to use an Apple product since the Apple ][ days was the support an encouragment of beautiful typography.  This is still true today in Apple Silicon Macs and iOS devices.

But it can be a tough and difficult learning curve.  Some of the issues that come up are what fonts will support the symbol set I wish to use?  Is that symbol set a standard or someone's hack?  How does my device know where to find the font?

I don't know that I can answer any of these question for you - doing the research is part of the problem domain.  I will share some of my experiences, mostly the successful experiences.

Those Kaktovik numerals over there... they are SF Symbols injected into Text object as Images, not a font at all, but they get many of the benefits of a font because of the Text container.

At this stage of development I had not seriously attempted the custom font route.

Experiment...

At some point it occured to me that I should try something in a Xcode Playground... just to test.  I did and it worked to prototype an idea.  Then I saw an article on using unicode characters in SwiftUI and tried that technique.  It failed but only my first few attempts.  Many of the failures appeared to be within my sphere of control.  So I kept at it.  With some success.

But wait... that's magic!  Playgrounds understands my Kaktovik unicode symbols - AWESOME!

I've not specified any FONT... it just works!

Why didn't that work so easy a year ago when I started this project?  Oh well; who can say... OS versions, things change.

From Playground to Project -> FAILED

My first attempt - just move the unicode character over and use them in a String (like I did in Playgrounds).  This FAILED!  But I didn't know why.  Why would the Xcode Playground just magically work but the iOS App not work?

One difference was Playground allowed the syntax of defining a String with the escaped Unicode charcter embedded.  Xcode project didn't like any of that - no sir - not type safe!  To get the compiler Type happy I had to define UInt32 values and then force unwrap the nil-able value from UnicodeScalar()! [note the !].  Wow!

let value_zero        : UInt32 = 0x1D2C0     // works in Swift App

let zero      =  String(UnicodeScalar(value_zero)!)     // works in Swift App

But all this still did NOT work.  Playground has something special.  What is it?

Specified Font

An early attempt to use a custom Font in the App.  Didn't appear to work at all.  I think this is what I attempted a year ago and got nothing to work.  Which sent me down the path of using SF Symbols.  The SF Symbols worked... but also had some significant rework and design changes to work well (have font like abilities of sizing).

The Secret 

Maybe this secret is going to shock you - maybe it's obviously stupid.  One has to INSTALL fonts into the iOS project & bundle the font file with the App.  Doho!

How does one do it?
Step 1: Install

First you go watch a few people talk about it on a video - then you learn by doing it... and getting lucky (or not getting unlucky).

Here's a good video:  CodeWithChris - How to Use Custom Fonts in SwiftUI.

Copy file into project space.
I created a NEW Group (folder) in Xcode's project space and then drag & drop the Font file (*.ttf most likely) into the folder.  Checking all the approprate boxes (watch Chris's video).

Declare the filename in Project : Info.
In the Info tab (use to be an actual info.plist file - now just a tab in your project settings) you must click the tiny plus button to add a NEW KEY & that key's name is "Fonts provided by application" it should pop in if you start with a capital 'F'.  This Key is an Array of Strings - next add the FileName with extension to the String for Item 0.

Step 2: Usage

Then in your code you can use the Font modifier to specify the ... a trick here ... PostScript Name in the detail tab of Font Book.  Finding this name can be a source of frustration... or trial and error.  Do not think that you will be so lucky as to have a font filename that is uniquely the exact same as it's PostScript Name... (I don't think spaces are allowed).

Footnote: Why Playgrounds Worked

If you followed along as well as I did... this story evloved over a year, and I lived it all.  The confounding thing was that Xcode Playgrounds would use the Kaktovik unicode symbols just fine - but the App did not.  That all comes back to gob-smack me because a year or more ago I installed to my Mac two Kaktovik fonts (two - because the first never worked & neither did the second).  But that is because I didn't do the Font file copy & Project Declaration in Info.  So the programs on the Mac had assess to a Kaktovik font file - but the iOS App in the simulators or real device never got a font file.
And if you think very hard ... you will note the touch of MAGIC that is still happening with Playgrounds.  There is never a declaration of a font to use!  POOF!