简体   繁体   中英

can't set custom font to uibutton programmatically

I'm trying to use ubuntu font for uibutton programmatically (button is also created programmatically and it's custom). Here's what I've done.

[self.titleLabel setFont:[UIFont fontWithName:@"Ubuntu" size:20.0]];

but no luck. I've checked everything.
- font is included in build phases
- I've added font in info.plist

But when I add label from interface builder and set ubuntu font from attribute inspector then everything works fine. Font is applied also on uibutton. but when I remove that label problem persists.
What's going on here?

Edit: I've already followed steps provided in answers of similar problems. like this.
How to include and use new fonts in iPhone SDK?

Just make sure all the fonts really are available in the bundle, try printing all fonts and check if you are using the correct name. You can very easily do this with following code in app delegate's didFinishLaunchingWithOptions method:

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

In Alphabetic Order:

for (NSString* family in [[UIFont familyNames] sortedArrayUsingSelector:@selector(compare:)])
{
  NSLog(@"%@", family);

for (NSString* name in [[UIFont fontNamesForFamilyName:family] sortedArrayUsingSelector:@selector(compare:)])
{
    NSLog(@"  %@", name);
 }
}

Get of your font from printed values and then use it as you are already doing. Hope it helps

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