1

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?

procterw
  • 131
  • 1
  • 8

2 Answers2

2

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)
    }
}
  • Why do we need to add this? Although it works, but it doesn't justify why we can't use normally – En Hui Lim Mar 31 '23 at 07:35
  • Yeah it sucks. We can however use custom fonts directly in xib or storyboard without above code block. Only guess I have is that iOS doesn't registers programmatic fonts automatically to save on app startup time. – Faizan Memon Apr 01 '23 at 08:30
0

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.

En Hui Lim
  • 85
  • 8