xcode 自定义字体在 iOS 8 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25950437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:38:29  来源:igfitidea点击:

Custom Font is not working in iOS 8

iosxcode

提问by CodeBrew

I have a custom font included in my project, which worked perfectly fine until I upgraded my Xcode to version 6 with iOS 8 as the base SDK. Now the custom font no longer works if I run the app on iOS 8 simulator or devices, but it still works in my iOS 7 devices. Any suggestions?

我的项目中包含一个自定义字体,在我使用 iOS 8 作为基本 SDK 将我的 Xcode 升级到版本 6 之前,它运行得非常好。现在,如果我在 iOS 8 模拟器或设备上运行该应用程序,自定义字体不再有效,但它仍然适用于我的 iOS 7 设备。有什么建议?

采纳答案by CodeBrew

I found the cause of the "missing" fonts. In fact the font is still there, but from iOS 7 to iOS 8 the font name had a subtle but abrupt change: in iOS 7 it is called, e.g. "Custom_Font-Style", but now in iOS 8 it is called "Custom-Font-Style". Notice the underscore between Custom and Font now changes to dash. Fortunately the font family name remains the same, as "Custom Font Family", so now instead of hard-coding the font name I have to extract it out from the font family, like this:

我找到了“丢失”字体的原因。实际上字体仍然存在,但是从 iOS 7 到 iOS 8,字体名称发生了微妙但突然的变化:在 iOS 7 中它被称为,例如“Custom_Font-Style”,但现在在 iOS 8 中它被称为“Custom-字体样式”。请注意 Custom 和 Font 之间的下划线现在更改为破折号。幸运的是,字体系列名称保持不变,与“自定义字体系列”相同,所以现在我不必硬编码字体名称,而是从字体系列中提取它,如下所示:

static NSString *_myCustomFontName;

+ (NSString *)myCustomFontName
{
    if ( !_myCustomFontName )
    {
        NSArray *arr = [UIFont fontNamesForFamilyName:@"Custom Font Family"];
        // I know I only have one font in this family
        if ( [arr count] > 0 )
            _myCustomFontName = arr[0];        
    }

   return _myCustomFontName;
}

I'm not sure how a font file presents its information, but now I guess my font file (custom font.ttf) only provides a font family name "Custom Font Family", and iOS derives its font name from certain rule, which for some reason changed from iOS 7 to iOS 8, so before we had Custom_Font-Style, now we have Custom-Font-Style.

我不确定字体文件如何显示其信息,但现在我猜我的字体文件(自定义字体.ttf)只提供了一个字体系列名称“自定义字体系列”,而 iOS 从某些规则派生其字体名称,这对于由于某些原因,从 iOS 7 到 iOS 8,所以之前我们有 Custom_Font-Style,现在我们有 Custom-Font-Style。

回答by mgm

In my case another solution worked. When I added the fonts to the project, they did not get automatically added to the compiled bundle resources. So I had to go 'Targets' -> 'Build Phases' -> 'Copy Bundle Resources'and add all the fonts manually. This did the trick

在我的情况下,另一个解决方案有效。当我将字体添加到项目中时,它们并没有自动添加到已编译的捆绑资源中。所以我不得不去'目标' - > '构建阶段' - > '复制捆绑资源'并手动添加所有字体。这成功了

回答by DJ van Wyk

As mentioned previously the way that the font name is read is different between iOS 8 and previous versions. I've had to put a check in to decide which name to use :

如前所述,在 iOS 8 和以前的版本之间读取字体名称的方式是不同的。我不得不检查以决定使用哪个名称:

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) {
    OS_SPECIFIC_GOTHIC_FONT = @"New";
} else {
    OS_SPECIFIC_GOTHIC_FONT = @"Highway Gothic";
}

You can use Finder, Get Info and Font Book to find the different names (iOS 8 uses the title in Font Book while the rest uses Full Name in Get Info)

您可以使用 Finder、Get Info 和 Font Book 来查找不同的名称(iOS 8 使用 Font Book 中的标题,其余使用 Get Info 中的 Full Name)

回答by Lars Blumberg

As http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/suggests, one should list all fonts available at runtime to see whether the custom font is available:

正如http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/建议的那样,应该列出运行时可用的所有字体,以查看自定义字体是否可用:

for family: String in UIFont.familyNames() {
    print("\(family)")
    for name: String in UIFont.fontNamesForFamilyName(family) {
        print("== \(name)")
    }
}

回答by Martin Roma?uk

Lars Blumberg answer in Swift 3

拉尔斯·布隆伯格回答 Swift 3

    for family in UIFont.familyNames {
        print("\(family)")
        for name: String in UIFont.fontNames(forFamilyName: family) {
            print("== \(name)")
        }
    }