问题描述
我的项目中包含一个自定义字体,在我使用 iOS 8 作为基础 SDK 将我的 Xcode 升级到版本 6 之前,它运行得非常好.现在,如果我在 iOS 8 模拟器或设备上运行该应用程序,自定义字体不再有效,但它仍然适用于我的 iOS 7 设备.有什么建议吗?
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 7 到 iOS 8,字体名称发生了微妙但突然的变化:在 iOS 7 中,它被称为,例如Custom_Font-Style",但现在在 iOS 8 中它被称为Custom-Font-Style".请注意 Custom 和 Font 之间的下划线现在更改为破折号.幸运的是,字体系列名称保持不变,与自定义字体系列"相同,因此现在我不必硬编码字体名称,而是从字体系列中提取它,如下所示:
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:
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;
}
我不确定字体文件如何呈现其信息,但现在我猜我的字体文件(自定义字体.ttf)只提供了一个字体系列名称自定义字体系列",而iOS根据某些规则派生其字体名称,由于某种原因,从 iOS 7 更改为 iOS 8,所以之前我们有 Custom_Font-Style,现在我们有 Custom-Font-Style.
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.
这篇关于自定义字体在 iOS 8 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!