自定义字体不能在Xcode中工作

我想在我的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字体显示为系统字体。 任何帮助将不胜感激。

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

编辑Info.plist:用“应用程序提供的字体”键添加一个新条目。

对于每个文件,将文件名添加到该arrays

在这里输入图像说明

在字体书中打开字体( 在查找器中双击你的字体 ),看看真正的文件名是什么,我看到这个:

在这里输入图像说明

现在将字体设置为您的标签

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

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

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

如果你的字体没有加载,那么下载新的字体并将其添加到你的项目中。 在downloding并join到我的项目工程。