[英]Custom Font not working in 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:使用键“应用程序提供的字体”添加一个新条目。
对于每个文件,将文件名添加到此数组
打开字体书中的字体(在finder中双击您的字体 )以查看真正的文件名,我看到了:
现在将字体设置为标签
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);
}
如果未加载您的字体,则下载新字体并将其添加到您的项目中。 在下载并添加到项目后对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.