ios iPhone 开发:如何使用自定义字体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4462852/
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
iPhone Development: how to use custom fonts?
提问by 001
I used a special font in my photoshop design,
我在我的photoshop设计中使用了一种特殊的字体,
is it possible to use these fonts natively in the iphone app? or
do I need to convert them into an image first?
If this is the only way to do it, what do you do with dynamic text?
是否可以在 iphone 应用程序中本地使用这些字体?或者
我需要先将它们转换成图像吗?
如果这是唯一的方法,您如何处理动态文本?
回答by edzio27
Copy your font file into Resources
In your application .plist create (if it's exist just create a row) a row called "Fonts provided by application" and then in "item 0" copy your font name for example "Ciutadella-Bold.otf" (if there are some spaces in font name, rename it and for example replace all spaces to minus '-')
Then you can define this font in your application:
UIFont *CiutadellaBold = [UIFont fontWithName:@"Ciutadella-Bold" size:17.0f];
And use in for instance in uiLabel:
[uiLabel setFont:CiutadellaBold];
将您的字体文件复制到资源中
在您的应用程序 .plist 中创建(如果它存在,只需创建一行)名为“应用程序提供的字体”的行,然后在“项目 0”中复制您的字体名称,例如“Ciutadella-Bold.otf”(如果有一些空格)在字体名称中,重命名它,例如将所有空格替换为减号“-”)
然后你可以在你的应用程序中定义这个字体:
UIFont *CiutadellaBold = [UIFont fontWithName:@"Ciutadella-Bold" size:17.0f];
例如在 uiLabel 中使用:
[uiLabel setFont:CiutadellaBold];
回答by Julio Gorgé
UIFont *font = [UIFont fontWithName:@"MyFont" size:20];
[label setFont:font];
Where "MyFont" would be a TrueType or OpenType file in your project (sans the file extension), and label would be an instance of UILabel.
其中“MyFont”将是您项目中的 TrueType 或 OpenType 文件(无文件扩展名),而 label 将是 UILabel 的一个实例。
回答by Tobias
Yes it is possible with iOS 3.2 and later. You need to have the fonts file, I forget what formats exactly. Add the font file as a resource to your project then add the names of the fonts in the applications info.plist in an array under the raw key 'UIAppFonts' or under the friendly key 'Fonts provided by application'.
是的,iOS 3.2 及更高版本是可能的。你需要有字体文件,我忘记了确切的格式。将字体文件作为资源添加到您的项目中,然后将应用程序 info.plist 中的字体名称添加到原始键“UIAppFonts”下或友好键“应用程序提供的字体”下的数组中。
Here's a sample application that you can look at - https://files.me.com/tobiasoleary/ey08n1. It prints out the all the fonts accessible to the application. Two fonts have been added Flames and Firestarter.
这是您可以查看的示例应用程序 - https://files.me.com/tobiasoleary/ey08n1。它打印出应用程序可访问的所有字体。添加了两种字体 Flames 和 Firestarter。
This is by far the easiest way to add custom fonts to application. If you need to support custom fonts for iOS before 4.0 see http://github.com/zynga/FontLabel/tree/master
这是迄今为止向应用程序添加自定义字体的最简单方法。如果您需要在 4.0 之前支持 iOS 的自定义字体,请参阅http://github.com/zynga/FontLabel/tree/master
This question has been asked before here: Can I embed a custom font in an iPhone application?.
这个问题之前曾在这里问过:Can I embed a custom font in an iPhone application? .
回答by Ayman
Note :
笔记 :
The parameter of “fontWithName” must be the real name of the font, not the name of the file. If you open your ttf file with the Mac Font Book, you will directly see its name on top of the window.
“fontWithName”的参数必须是字体的真实名称,而不是文件名。如果您使用 Mac Font Book 打开 ttf 文件,您将直接在窗口顶部看到其名称。