iOS 7 中的自定义字体 [英] Custom fonts in iOS 7

查看:21
本文介绍了iOS 7 中的自定义字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款游戏,我想在我的应用中使用自定义字体.如果这很重要,我正在使用 SpriteKit.我试过使用这个 https://github.com/deni2s/IBCustomFonts 但我无法得到它使用这种字体 http://www.fontspace.com/freaky-fonts/emulogic

I'm developing a game and I would like to use a custom font in my app. I'm using SpriteKit for it, if that's important. I've tried using this https://github.com/deni2s/IBCustomFonts but I cannot get it to work with this font http://www.fontspace.com/freaky-fonts/emulogic

我尝试了各种方法,我已经在应用程序的 info.plist 上注册了字体...有谁知道发生了什么?

I've tried various things, and I've registered the font on the info.plist of the app... Does anyone know what's going on?

推荐答案

首先,我假设 SpriteKit 没有任何区别.

First of all I'm assuming that SpriteKit doesn't make any difference.

  1. 您需要将 .otf 或 .ttf 中的字体复制到您的项目中.例如在支持文件中.
  2. 您需要编辑 .plist 文件.将 应用程序提供的字体" 键添加到您的 plist 中,并在 Item 0 中将您复制的字体的确切文件名复制到带有扩展名的支持文件中.例如:JosefinSansStd-Light_0.otf"
  3. 确保您导入到应用中的字体已打包到应用本身中.为此,请依次选择目标构建阶段复制捆绑资源.如果您在其中没有看到您的字体,请将其从支持文件中拖出.
  1. You need your font in .otf or .ttf copied to your project. For example in Supporting Files.
  2. You need to edit .plist file. Add "Fonts provided by application" key into your plist and in Item 0 copy the exact filename of the font you copied to your Supporting files WITH extension. For example: "JosefinSansStd-Light_0.otf"
  3. Make sure that the font you imported to your app is being packed into app itself. Do that by selecting your Target, then Build Phases, then Copy Bundle Resources. If you don't see your font in there, drag it from Supporting Files.

最后,您希望在应用程序启动时列出所有字体,以便查看字体的可用名称.你会用这段代码做到这一点:

Finally, you would like to list all your fonts when the app starts just to see useable name for your font. You will do that with this little piece of code:

NSArray *fontFamilies = [UIFont familyNames];
for (int i = 0; i < [fontFamilies count]; i++)
{
    NSString *fontFamily = [fontFamilies objectAtIndex:i];
    NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
    NSLog (@"%@: %@", fontFamily, fontNames);
}

在打印结果中搜索您的字体,例如,我会搜索Josefin",我会看到实际的字体名称是JosefinSansStd-Light".之后,您只需要通过以下方式使用该字体:

Search for your font in printed results, for example, I would search for "Josefin" and I would see that actual font name is "JosefinSansStd-Light". After that you only need to use that font by:

UIFont *customFont = [UIFont fontWithName:@"JosefinSansStd-Light" size:20];

在 iOS8 中,您将您的字体直接添加到项目中,它们在界面构建器中可见.修改您的代码以解决此问题,但以编程方式为 iOS7 设置字体并在 xCode6 界面构建器中选择它.附注.xCode6 中的界面构建器为您提供了正确的字体名称,您可以将其复制粘贴到下面的代码中.

In iOS8 you add your fonts directly to the project and they are visible in the interface builder. Modify your code to account for this but programmatically setting font for iOS7 and selecting it in xCode6 interface builder. PS. Interface builder in xCode6 gives you the correct font name that you can copy-paste into the code below.

#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)

 if(SYSTEM_VERSION_LESS_THAN(@"8.0"))
    {
        UIFont *customFont = [UIFont fontWithName:@"OpenSans-Light" size:32];
        self.registerLabel.font = customFont;  
    }

希望这会有所帮助,加油.

Hope this helps, cheers.

这篇关于iOS 7 中的自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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