【问题标题】:Custom Font not working in Xcode自定义字体在 Xcode 中不起作用
【发布时间】:2015-07-20 14:29:56
【问题描述】:

我正在尝试在我的 iOS 应用程序中使用 truetype (.ttf) 字体,但它无法正常工作。我已将字体添加到我的项目中并正确添加了目标。我在应用程序提供的字体下的 info.plist 中添加了字体。我正在使用 NSAttributedString 来显示文本,这是我的代码

UIFont *hrFont = [UIFont fontWithName:@"ocrb" size:18.0];
NSDictionary *hrDict = [NSDictionary dictionaryWithObject:hrFont forKey:NSFontAttributeName];
NSMutableAttributedString *hrAttrString = [[NSMutableAttributedString alloc] initWithString:hrString attributes: hrDict];

UIFont *barcodeFont = [UIFont fontWithName:@"IDAutomation DataBar 34" size:22];
NSDictionary *barcodeDict = [NSDictionary dictionaryWithObject:barcodeFont forKey:NSFontAttributeName];
NSMutableAttributedString *barcodeAttrString = [[NSMutableAttributedString alloc]initWithString: alphaOnly attributes:barcodeDict];

[hrAttrString appendAttributedString:barcodeAttrString];

条形码字体正确显示,但 ocr 字体显示为系统字体。任何帮助将不胜感激。

【问题讨论】:

    标签: ios objective-c xcode fonts


    【解决方案1】:

    将您的自定义字体添加到您的项目中,即拖动字体 文件(ocrb.TTF) 到 XCode 项目中。

    Edit Info.plist: 添加一个新条目,键为 "Fonts provided by 应用程序”

    对于您的每个文件,将文件名添加到此数组中

    在字体册中打开字体(在查找器中双击您的字体) 看看真正的文件名是什么,我看到了:

    现在为您的标签设置字体

    yourLabel.font = [UIFont fontWithName:@"ocrb" size:15];
    

    【讨论】:

    • 我必须在代码中编写才能真正调用字体的是 [UIFont fontWithName:@"OCRB" size:15];出于某种未知的原因。但它最终完美地解决了。谢谢
    【解决方案2】:

    您可以使用以下代码检查该字体是否已加载

    NSArray *fontFamilies = [UIFont familyNames];
    
    for (int i = 0; i < [fontFamilies count]; i++)
    {
        NSString *fontFamily = [fontFamilies objectAtIndex:i];
        NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
        NSLog (@"%@: %@", fontFamily, fontNames);
    }
    

    如果您的字体未加载,请下载新字体并将其添加到您的项目中。下载并添加到项目后对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 2012-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多