Custom Font not working in Xcode

纵然是瞬间 提交于 2020-01-06 06:59:12

问题


I am trying to use a truetype (.ttf) font in my iOS app and it is not working. 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. I am using an NSAttributedString to display text and this is my code

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. Any help would be greatly appreciated.


回答1:


Add your custom font into your project , i.e. Dragged the font file(ocrb.TTF) into XCode project.

Edit Info.plist: Add a new entry with the key "Fonts provided by application".

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:

Now set font to your label

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



回答2:


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.



来源:https://stackoverflow.com/questions/31509894/custom-font-not-working-in-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!