UIFontDescriptor can't find iOS font

310 views Asked by At

I am trying to use Avenir Next on a NSAttributedString w/ UIFontDescriptor. It is an in-built iOS font. It works in my SwiftUI components but if I try to create a custom UIFontDescriptor it doesn't find the font and defaults to SFUI. Why is this and how can I fix it?

// descriptor shows font as SFUI
let boldDescriptor = UIFontDescriptor(fontAttributes: [
    UIFontDescriptor.AttributeName.family: "Avenir Next",
    UIFontDescriptor.AttributeName.traits: [
        UIFontDescriptor.TraitKey.weight: UIFont.Weight.bold
    ]
])
1

There are 1 answers

0
ankushlokhande On

I'm not sure, How you are using this descriptor method.

Avenir Next is an in-built iOS font, but it may not be available on all devices. Make sure the font is installed on the device you are testing on.
Check font is available by using the UIFont.fontNames(forFamilyName: "Avenir Next") method to get a list of available font names for the Avenir Next family.

var fonts: String = UIFont.fontNames(forFamilyName: "Avenir Next")
print(fonts) // ["AvenirNext-Regular", "AvenirNext-Italic", "AvenirNext-UltraLight", "AvenirNext-UltraLightItalic", "AvenirNext-Medium", "AvenirNext-MediumItalic", "AvenirNext-DemiBold", "AvenirNext-DemiBoldItalic", "AvenirNext-Bold", "AvenirNext-BoldItalic", "AvenirNext-Heavy", "AvenirNext-HeavyItalic"]

I also created a new project for boldDescriptor method & it is working fine in my case.
Use this project Demo Project for UIFontDescriptor - GitHub & let me know if this example doesn't work for you.

Output:
Output Screenshot

Also, you can import font from the system into the project.

Please also check the following reasons:

  • Make sure your System is up to date because many of the Catalina users suffered from this issue.
  • Make sure you're using iOS 13 or greater version for testing the fonts. Note on official website
  • Make sure the required font is available in the Font Book. Search by Font Book or Search by folder.
  • It is unlikely that fonts would just "stop working" on their own. More likely, there is an issue with the operating system upgrade, or the font databases and caches have been damaged during the upgrade process. It is also important to only have one font manager installed on your Mac at a time, as using multiple font managers can lead to conflicts and damage to the underlying font databases and caches. If you are experiencing issues with fonts not working properly, you may need to try repairing or rebuilding the font databases and caches.
  • You should be able to resolve any duplicate fonts if available in your system and ensure that only the necessary fonts are installed and active. Resolve Duplicates