NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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 and view the Font Info (⌘I), you will see each font has several names: 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

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.


Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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.