【问题标题】:Xcode & Objective-C: Can't display Bold on a custom font from a ZIP fileXcode 和 Objective-C:无法在 ZIP 文件中的自定义字体上显示粗体
【发布时间】:2020-02-24 08:26:44
【问题描述】:

在这个应用程序中,我们解压缩一个包含字体文件夹的文件。在该文件夹中,我们有一些 ttf 格式的自定义字体。 然后我们在标签和按钮中显示这个自定义字体,它可以正常工作,但 BOLD 除外。 事实上,粗体被标准字体取代。

这是加载字体的代码:

- (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;
     }

最后是设置 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];

}

我是不是做错了什么,或者可能无法为自定义字体显示 BOLD?

最好的问候。

【问题讨论】:

  • 代码太复杂。为什么需要手动加载字体?为什么不直接在 plist 中注册呢?为什么你使用字体描述符?只需使用 fontWithName: 获取字体
  • @Cy-4AH 这是一个复杂的应用程序。只要知道我们需要按照我上面指定的方式加载。在 Fonts 文件夹中,我们有例如“Angelina.ttf”,我们正在尝试在需要时将其设为粗体。我们怎么知道什么时候需要?来自同一 ZIP 中包含的 XML 文件。
  • 我认为你只需要在 xml 文件中放入完整的字体名称而不是标志
  • @Cy-4AH 问题似乎是我们错过了“Angelina-Bold”。如果我们手动设置它仍然无法正常工作。
  • @Lifetronic - 暂时忽略您的setLabel: 方法... 您的字体文件夹中有哪些.ttf 文件?当您调用loadFont: 加载“粗体”版本时,您是否获得了有效的新粗体字体?如果没有,请通过loadFont: 看看它失败的地方。

标签: objective-c xcode fonts bold


【解决方案1】:

我可以通过将自定义字体的粗体版本放入 Fonts 文件夹并在必要时加载它来解决此问题。

因此,如果您需要在 iOS 中为自定义字体添加粗体,则必须获取该字体的单独粗体版本才能使其正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-02
    • 2015-12-21
    • 2021-08-06
    • 1970-01-01
    • 2011-10-08
    • 2011-02-12
    • 1970-01-01
    • 2015-12-11
    相关资源
    最近更新 更多