0
votes

I have a custom built font that I've added to the Xcode project. I've followed all the instructed steps in adding the font and when I set a UILabel or UITextField as the custom font in IB it works great, however when I set the custom font at runtime it appears as the iOS default font instead. I've checked the UIAppFonts list and have them print out with NSLog where it shows the full name so I know that when I call [UIFont fontWithName:@"font name" size:s] it's the correct name. What could be the issue?

The font name finding method below (This method provided the incorrect names to use in custom UIFont instantiation):

NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];
NSArray* fontFiles = [infoDict objectForKey:@"UIAppFonts"];

for (NSString *fontFile in fontFiles) {
    NSURL *url = [[NSBundle mainBundle] URLForResource:fontFile withExtension:NULL];
    NSData *fontData = [NSData dataWithContentsOfURL:url];
    CGDataProviderRef fontDataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)fontData);
    CGFontRef loadedFont = CGFontCreateWithDataProvider(fontDataProvider);
    NSString *fullName = CFBridgingRelease(CGFontCopyFullName(loadedFont));
    CGFontRelease(loadedFont);
    CGDataProviderRelease(fontDataProvider);
    NSLog(@"FULL NAME:%@",fullName);
}
1
It's hard to say without more information. I would store your instance of the font in a variable and examine the object in the debugger. Is the font getting built correctly at that point if you inspect the font family?Ben M
Thanks, that made sense and indeed it is nil with code: UIFont * font = [UIFont fontWithName:@"font name" size:24.0f];Silverstar

1 Answers

1
votes

Try executing this code:

for (NSString* family in [UIFont familyNames]) {
    NSLog(@"%@", family);

    for (NSString* name in [UIFont fontNamesForFamilyName: family]) {
        NSLog(@"  %@", name);
    }
}

Do you see the font name you are trying to use?