简体   繁体   English

无法通过编程将自定义字体设置为uibutton

[英]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). 我正在尝试以编程方式对uibutton使用ubuntu字体(button也以编程方式创建,并且是自定义的)。 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 -我在info.plist中添加了字体

But when I add label from interface builder and set ubuntu font from attribute inspector then everything works fine. 但是,当我从界面构建器添加标签并从属性检查器设置ubuntu字体时,一切正常。 Font is applied also on uibutton. 字体也应用在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? 如何在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: 您可以使用应用程序委托的didFinishLaunchingWithOptions方法中的以下代码轻松实现此目的:

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 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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