UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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)?

1      

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.

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.