简体   繁体   中英

Xcode & Objective-C: Can't display Bold on a custom font from a ZIP file

In this app we unzip a file which contains a Fonts folder. Inside that folder we have some custom font in ttf format. We then display this custom font in labels and buttons and it works properly except that for BOLD. Indeed, the bold get replaced by a standard font.

This is the code to load font:

- (UIFont*)loadFont:(NSString *)withName :(int)systemIndex :(CGFloat)size :(UIFont*) dftFont
{

    if ([self isAppleFont:withName]) {
         return [UIFont fontWithName:withName size:size];
    }


    if ([fontsDictionary objectForKey:withName]) {
        return [fontsDictionary objectForKey:withName];
    }

     NSFileManager *fileManager = [NSFileManager defaultManager];
     NSString* myString = [NSString stringWithFormat:@"%@.ttf", withName];
     NSString* fontsFolderPath = [SystemsManager fontsFolderForSystemIndex:systemIndex];
     NSString* completeFontName = [NSString stringWithFormat:@"%@/%@", fontsFolderPath,myString];

     if ([fileManager fileExistsAtPath:fontsFolderPath] && [fileManager fileExistsAtPath:completeFontName]) {

         NSData *data = [NSData dataWithContentsOfFile:completeFontName];
         CFErrorRef error;
         CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);
         CGFontRef font = CGFontCreateWithDataProvider(provider);

         if(!CTFontManagerRegisterGraphicsFont(font, &error)){
             CFStringRef errorDescription = CFErrorCopyDescription(error);
             NSLog(@"Failed to load font: %@", errorDescription);
         }

         CTFontRef ctFont = CTFontCreateWithGraphicsFont(font, size, NULL, NULL);
         //CTFontRef ctFont = CTFontCreateWithGraphicsFont(font, 0, NULL, NULL);
         UIFont *uiFont = CFBridgingRelease(ctFont);

         [fontsDictionary setObject:uiFont forKey:withName];

         return uiFont;
     } else {
          return dftFont;
     }

And finally the code that set the BOLD.

-(void)setLabel:(NSString*)bold :(NSString*)italic :(UILabel*)label :(CGFloat*)withSize {

    BOOL isBold = ![bold isEqualToString:@"REGULAR"];
    BOOL isItalic = ![italic isEqualToString:@"NonItalic"];

    UIFontDescriptor * fontBold = [label.font.fontDescriptor
    fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];

    UIFontDescriptor * fontItalic = [label.font.fontDescriptor
    fontDescriptorWithSymbolicTraits: UIFontDescriptorTraitItalic];

    UIFontDescriptor * fontBoldItalic = [label.font.fontDescriptor
    fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold
                    | UIFontDescriptorTraitItalic];


   if ( isBold && isItalic )
    {
        label.font = [UIFont fontWithDescriptor:fontBoldItalic size:0];

    }
    if (isBold && !isItalic )
    {
        label.font = [UIFont fontWithDescriptor:fontBold size:0];
    }
     if (isItalic)
    {
        label.font = [UIFont fontWithDescriptor:fontItalic size:0];
    }

    label.font = [label.font fontWithSize:*withSize];

}

Did I make something wrong or maybe is not possibile to display BOLD for custom fonts?

Best regards.

I could solve the issue with by putting a Bold version of the custom font into the Fonts folder and loading it when necessary.

So if you need a bold for a custom font in iOS you have to get a separate bold version of that font to make it works.

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