CodePlumber CodePlumber - 2 months ago 7
iOS Question

Custom Font is not working in iOS 8

I have a custom font included in my project, which worked perfectly fine until I upgraded my Xcode to version 6 with iOS 8 as the base SDK. Now the custom font no longer works if I run the app on iOS 8 simulator or devices, but it still works in my iOS 7 devices. Any suggestions?

Answer

I found the cause of the "missing" fonts. In fact the font is still there, but from iOS 7 to iOS 8 the font name had a subtle but abrupt change: in iOS 7 it is called, e.g. "Custom_Font-Style", but now in iOS 8 it is called "Custom-Font-Style". Notice the underscore between Custom and Font now changes to dash. Fortunately the font family name remains the same, as "Custom Font Family", so now instead of hard-coding the font name I have to extract it out from the font family, like this:

static NSString *_myCustomFontName;

+ (NSString *)myCustomFontName
{
    if ( !_myCustomFontName )
    {
        NSArray *arr = [UIFont fontNamesForFamilyName:@"Custom Font Family"];
        // I know I only have one font in this family
        if ( [arr count] > 0 )
            _myCustomFontName = arr[0];        
    }

   return _myCustomFontName;
}

I'm not sure how a font file presents its information, but now I guess my font file (custom font.ttf) only provides a font family name "Custom Font Family", and iOS derives its font name from certain rule, which for some reason changed from iOS 7 to iOS 8, so before we had Custom_Font-Style, now we have Custom-Font-Style.

Comments