自定义字体显示在IB中,但不在设备上 [英] Custom font showing up in IB but not on device

查看:124
本文介绍了自定义字体显示在IB中,但不在设备上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Open Sans系列的字体。我添加了字体到我的Xcode项目,我检查了他们被添加到我的应用程序的资源包,我添加到我的Info.plist文件的字体。



当我编辑界面生成器中的XIB,当在字体下拉菜单中选择自定义时,我可以在 UILabel 上使用Open Sans字体。字体在界面生成器中的预览上正确呈现,但是当我在设备上启动应用程序时,字体不会被应用到标签。



字体编程,但也没有工作。我没有收到任何警告和错误。



我忘了能够使用自定义字体吗?



在运行iOS7的iPad Air上以及在运行iOS8的iPhone 6上,这种行为一直是相同的。 确保包中的所有字体都可用,请尝试打印所有字体并检查是否使用了正确的名称。
您可以使用以下代码非常简单地在代码的didFinishLaunchingWithOptions方法中执行此操作:

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


$ / code $ / pre

或在Swift中:

  if let familyNames = UIFont.familyNames()as? [String] {
在familyNames中为familyName {
println(Family:+ familyName)
如果让fontNames = UIFont.fontNamesForFamilyName(familyName)为? fontName {
println(\ tFont:+ fontName)
}
}
}
}

swift代码不是最高效的,但是应该用来检查包中是否存在字体。 / p>

I'm trying to use fonts from the Open Sans family. I added the fonts to my Xcode project, I checked that they were added to my application's resources bundle and I added the fonts to my Info.plist file.

When I edit a XIB in Interface Builder, I can use Open Sans font on UILabels when selecting Custom in the font dropdown menu. The font are correctly rendered on the preview in Interface Builder, but then when I launch the application on the device, the font is not applied to the labels.

I tried setting the font programmatically, but that didn't work either. And I'm not getting any warning nor error.

What did I forgot to be able to use a custom font?

The behavior has been the same on an iPad Air running iOS7 and on an iPhone 6 running iOS8.

解决方案

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);
    }
}

or in Swift:

if let familyNames = UIFont.familyNames() as? [String] {
    for familyName in familyNames {
        println("Family : " + familyName)
        if let fontNames = UIFont.fontNamesForFamilyName(familyName) as? [String] {
            for fontName in fontNames {
                println("\tFont : " + fontName)
            }
        }
    }
}

The swift code is not the most efficient, but should work for checking if the fonts exist in the bundle.

这篇关于自定义字体显示在IB中,但不在设备上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆