uifont 'Impact' not working for iOS

爱⌒轻易说出口 提交于 2019-12-14 02:02:12

问题


I'm trying to use Impact.ttf. It is in built in OSX, but can't see it in custom fonts on xcode. So I included it in my project as shown in screens and used

UIFont *font1 = [UIFont fontWithName:@"Impact.ttf" size:50];
NSLog(@"%@",font1);
self.labelTop.font = font1;

The Log is showing null value in font1. Plz help. How to include the font.


回答1:


Check Target membership of the font(.ttf) files.




回答2:


First of all Add font .ttf file to Project then follow step

Step 1 : Click on ProjectName in Navigation Panel on Left Side
Step 2 : Go to Info Tab
Step 3 : Add New Key with name "Fonts provided by application" by Pressing "+" button
Step 4 : Set value for that key : Impact.ttf . its your font file name with extension
Step 5 : now you can use your custom font as follow

[textLabel setFont:[UIFont fontWithName:@"Impact" size:12.0f]];



回答3:


As I said in this answer, the filename or the fontname might be different than the name that iOS likes to see in fontWithName:. Use Fontbook on your Mac to get the right name, if it still does not work after you have changed the filenames in the .plist. You can also enumerate through all fonts to find the right name by using the following code:

for(NSString *familyName in [UIFont familyNames]) {
   for(NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
     NSLog(@"%@", fontName);
   }
}



回答4:


This is Step for, How to add custom font in Application.

1 - Add .TTF font in your application
2 - Modify the application-info.plist file.
3 - Add the key "Fonts provided by application" to a new row
4 - and add each .TTF file (of font) to each line.

For more info read This and This site.

FOR MOREINFORMATION :

For Bold

// Equivalent to [UIFont fontWithName:@"FontName-BoldMT" size:17]
UIFont* font = [UIFont fontWithFamilyName:@"FontName" traits:GSBoldFontMask size:17];

And bold/italic

UIFont* font = [UIFont fontWithMarkupDescription:@"font-family: FontName; font-size: 17px; font-weight: bold/italic;"]; // set here, either bold/italic.


来源:https://stackoverflow.com/questions/15597905/uifont-impact-not-working-for-ios

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