skip to Main Content

I’m unable to get any custom fonts working based on the instructions in Apple’s docs and stack overflow answers. Here is my setup that I would expect to work:

1) When copying font files into project:

enter image description here

2) Font .ttf files are added to project

enter image description here

3) Target membership is selected

enter image description here

4) Font names added to UIAppFonts in info.plist

    <key>UIAppFonts</key>
    <array>
        <String>SourceSerifPro-Black.ttf</String>
        <String>SourceSerifPro-BlackItalic.ttf</String>
        <String>SourceSerifPro-Bold.ttf</String>
        <String>SourceSerifPro-BoldItalic.ttf</String>
        <String>SourceSerifPro-ExtraLight.ttf</String>
        <String>SourceSerifPro-ExtraLightItalic.ttf</String>
        <String>SourceSerifPro-Italic.ttf</String>
        <String>SourceSerifPro-Light.ttf</String>
        <String>SourceSerifPro-LightItalic.ttf</String>
        <String>SourceSerifPro-Regular.ttf</String>
        <String>SourceSerifPro-SemiBold.ttf</String>
        <String>SourceSerifPro-SemiBoldItalic.ttf</String>
    </array>

5) Fonts are present under Copy Bundle Resources

enter image description here

6) All system fonts are logged, but new fonts are not found

for family in UIFont.familyNames.sorted() {
    let names = UIFont.fontNames(forFamilyName: family)
    print("Family: (family) Font names: (names)")
}

7) Tried using the font but only seeing the default font

Text("Test text")
  .font(.custom("SourceSerifPro-Regular", size: 20))

I’ve also erased and restarted the simulator and restart my computer and Xcode multiple times. Any idea what else I may be missing?

2

Answers


  1. I had ran into similar problem. Try calling this function once before using any custom font.

    private func registerCustomFonts() {
        let fonts = Bundle.main.urls(forResourcesWithExtension: "ttf", subdirectory: nil)
        fonts?.forEach { url in
            CTFontManagerRegisterFontsForURL(url as CFURL, .process, nil)
        }
    }
    
    Login or Signup to reply.
  2. Adding custom font to Xcode 13+

    Do take note. I made the mistake not adding the extension.ttf so it didn’t import. But otherwise it should.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search