[英]Custom Font not working in Xcode
I am trying to use a truetype (.ttf) font in my iOS app and it is not working. 我正在尝试在我的iOS应用程序中使用truetype(.ttf)字体,但无法正常工作。 I have added the font to my project and added the target correctly. 我已将字体添加到我的项目中,并正确添加了目标。 I added the font in the info.plist under fonts provided by application. 我在应用程序提供的字体下的info.plist中添加了该字体。 I am using an NSAttributedString to display text and this is my code 我正在使用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];
The barcode font displays correctly but the ocr font displays as the system font instead. 条形码字体正确显示,但是ocr字体显示为系统字体。 Any help would be greatly appreciated. 任何帮助将不胜感激。
Add your custom font into your project , ie Dragged the font file( ocrb.TTF ) into XCode project. 将自定义字体添加到项目中, 即将字体文件( ocrb.TTF )拖入XCode项目中。
Edit Info.plist: Add a new entry with the key "Fonts provided by application" . 编辑Info.plist:使用键“应用程序提供的字体”添加一个新条目。
For each of your files, add the file name to this array 对于每个文件,将文件名添加到此数组
Opened the font in font book( double click on your font in finder ) to see what the real filename is and I see this: 打开字体书中的字体(在finder中双击您的字体 )以查看真正的文件名,我看到了:
Now set font to your label 现在将字体设置为标签
yourLabel.font = [UIFont fontWithName:@"ocrb" size:15];
You can check whether that font is loaded or not by using the following code 您可以使用以下代码检查是否加载了该字体
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);
}
If your font is not loaded then download new font & add it into your project. 如果未加载您的字体,则下载新字体并将其添加到您的项目中。 After downloding & adding into project works for me. 在下载并添加到项目后对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.