简体   繁体   中英

Custom font in XCode 4.3.2

I know this subject has been run into the ground, however I have tried every solution I've found on StackOverflow to fix this issue, and its just not working for some reason. I am creating a UILabel programmatically, then using this font. The UILabel is rendering but not in the font I want it to (looks like its defaulting to something)

First I imported two .ttf fonts into the xcode project under a folder called "Resources". The two fonts are Gotham Book and Gotham Book Italic (GothaBoo.ttf, GothaBooIta.ttf)

Then, I added a line to the [my app]-Info.plist file called "UIAppFont", added two rows to the array, Item 0 | String | GothaBoo, Item 1 | String | GothaBooIta.

I checked the Target Build Phases, under Copy Bundle Resources, and made sure the two fonts were in there.

Then, I simply added this code to the viewDidLoad routine:

UILabel *myLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
myLabel1.text = @"Hello";
myLabel1.backgroundColor = [UIColor clearColor];
myLabel1.font = [UIFont fontWithName: @"GothaBoo" size: 48.0];
myLabel1.textColor = [UIColor blackColor];

[self.view addSubview:myLabel1]; 

No dice. What on earth am I doing wrong? The project is in Xcode 4.3.2, for the iPad, running 5+ of iOS

In the array the full filenames must be provided, so instead of Item 0 | String | GothaBoo, Item 1 | String | GothaBooIta you will have to turn this into Item 0 | String | GothaBoo.ttf, Item 1 | String | GothaBooIta.ttf

Secondly 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.

Beware on the difference of the filename and the fonts name. The fonts name will be found in Fontbook. In info.plist, it's the filename, and in the code, it's the fonts name.

Struggled with this for a long time myself. Pretty sure that would work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM