PIXEL
DOCK

I like the smell of Swift in the morning…

UIFont: Problem when using more than 2 custom fonts of the same Font Family.

Posted: | Author: | Filed under: iOS | Tags: , | 4 Comments »

When you add custom fonts to your iPhone or iPad App you might experience the following problem: If you add more than 2 font variants of the same font family (e.g. “Normal”, “Bold” and “Extended”), only the last two font variants that you added to the project will be usable. The first variant (the one on top of your list) just won’t show up and is replaced by one of the two variants that were added last to the project.

The reason is quite simple: The iOS SDK apparently allows only 2 custom fonts of the same font family!

As a solution you could change the font-family of your font, so that not more than two font variants have the same font-family. Just to be clear: it’s not enough to change the file name of your font. You have to change all the references to the font-family inside your font files.

You can do this with with fontforge. Fontforge requires that you installed the X11 server on your system, but that should not be a problem as it is automatically installed when you install Xcode.

Make sure that you find all references to the font family name after you opened the font in Fontforge:

  1. Open your Font in Fontforge
  2. Goto ‘Element’ -> ‘Font Info’ and change the ‘Family Name’ field
  3. Goto ‘Element’ -> ‘TTF Names’ and change the fields ‘Family’ and ‘Preferred Family’
  4. Goto ‘File’ -> ‘Generate Fonts’ and save your edited font

With this little trick you should be able to use more than two variants of one font.

4 Comments

  • 1

    Mike N.said at

    I found this is now happening when trying to use just two variants of the same font. Have you ran into that? In my project I’m only able to use one variant. This is a new issue for iOS 8 as I’ve written many apps for older versions of iOS that have used two variants of the same font.

  • 2

    Jörnsaid at

    Hi Mike,

    thanks for your comment. Have you had a look at the font names under iOS8? You can use this to print out all the font names known to the system:

    for (NSString *familyName in [UIFont familyNames]) {
    for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
    NSLog(@”%@”, fontName);
    }
    }

    It seems that iOS8 has some minor changes in the font names that will lead to those fonts not being displayed. Sometimes font names that contain underscores under iOS7 now contain contain hyphens. So if you run that code under iOS7 and then under iOS8 and compare the names, you might see some changes that might be the reason for your issue.

    Please let me know if you found the solution.

    Best regards,

    Jörn

  • 3

    Custom Font in ios not working - QuestionFocussaid at

    […] See blog post here for details and solution : http://www.pixeldock.com/blog/uifont-problem-when-using-more-than-2-custom-fonts-of-the-same-font-fa… […]

  • 4

    ¿Ciertas fuentes no aparecen? - Fallosweb.comsaid at

    […] Una solución alternativa y más complicada de pixeldock.com: […]


Leave a Reply