WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Need help figuring out the correct way to implement "custom" fonts in SwiftUI coding

Forums > SwiftUI

I'm having difficulty figuring out how to name fonts dragged into an Xcode project: spaces seem to matter... sometimes, using the font weight in the name works... sometimes.... Here's a bunch of them in with different coding/spelling/spacing/whatever (I'm not sure if the images will appear, so I've also inserted links to the files on my OneDrive):

Xcode Xcode

Font Files Font Files

  1. Why does the space between "Cinzel" and "Decorative" (lines 3 & 4) matter and the space between "Indie" and "Flower" (lines 8 & 9) not matter?
  2. Why does appending the word "Regular" to "Send Flowers" (line 10) work but appending the word "Regular" to "Indie Flower" (line 6) not work?
  3. Is there a preferred/safer method to setting the weight of non-"Regular" fonts: using .fontWeight (line 1) vs. appending the weight to the name (line 2)?

   

If you install your fonts into FontBook.app and view the Font Info (⌘I), you will see each font has several names:

FontBook.app example

It looks like .custom(_:size:) will recognize the PostScript name and the Full name, with the additional wrinkle of not caring whether or not you include " Regular" if it's part of the name. e.g., Cinzel Decorative Regular and Cinzel Decorative will both result in using CinzelDecorative-Regular.

To illustrate:

List {
    Text("CinzelDecorative-Regular\n(PostScript name)")
        .font(.custom("CinzelDecorative-Regular", size: 14))
    Text("Cinzel Decorative Regular\n(Full name)")
        .font(.custom("Cinzel Decorative Regular", size: 14))
    Text("Cinzel Decorative\n(Full name without Regular)")
        .font(.custom("Cinzel Decorative", size: 14))
}

custom fonts in Xcode simulator

For IndieFlower-Regular, the PostScript name is IndieFlower and the Full name is Indie Flower, so either of those will work. But IndieFlower Regular and Indie Flower Regular will not work.

IndieFlower in FontBook.app

List {
    Text("IndieFlower\n(PostScript name)")
        .font(.custom("IndieFlower", size: 14))
    Text("Indie Flower\n(Full name)")
        .font(.custom("Indie Flower", size: 14))
    Text("IndieFlower Regular")
        .font(.custom("IndieFlower Regular", size: 14))
    Text("Indie Flower Regular")
        .font(.custom("Indie Flower Regular", size: 14))
}

IndieFlower example

Apple says to use the PostScript name when applyng a custom font, so that's probably a good idea, even if .custom(_:size:) does recognize the Full name.

   

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.