简体   繁体   中英

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?

In my case another solution worked. When I added the fonts to the project, they did not get automatically added to the compiled bundle resources. So I had to go 'Targets' -> 'Build Phases' -> 'Copy Bundle Resources' and add all the fonts manually. This did the trick

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, eg "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.

As mentioned previously the way that the font name is read is different between iOS 8 and previous versions. I've had to put a check in to decide which name to use :

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) {
    OS_SPECIFIC_GOTHIC_FONT = @"New";
} else {
    OS_SPECIFIC_GOTHIC_FONT = @"Highway Gothic";
}

You can use Finder, Get Info and Font Book to find the different names (iOS 8 uses the title in Font Book while the rest uses Full Name in Get Info)

As http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/ suggests, one should list all fonts available at runtime to see whether the custom font is available:

for family: String in UIFont.familyNames() {
    print("\(family)")
    for name: String in UIFont.fontNamesForFamilyName(family) {
        print("== \(name)")
    }
}

Lars Blumberg answer in Swift 3

    for family in UIFont.familyNames {
        print("\(family)")
        for name: String in UIFont.fontNames(forFamilyName: family) {
            print("== \(name)")
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM