我想有一个应用程序包括一个自定义字体渲染文本,加载它,然后使用它与标准UIKit元素如UILabel。这可能吗?


当前回答

是的,您可以包括自定义字体。参考UIFont的文档,特别是fontWithName:size:方法。

1)确保在你的资源文件夹中包含了该字体。

2)字体的“名称”不一定是文件名。

3)确保你有使用这种字体的合法权利。通过把它包含在你的应用中,你也在传播它,你需要有这样做的权利。

其他回答

如果你使用的是xcode 4.3,你必须将字体添加到拷贝Bundle资源下的构建阶段,根据线程https://stackoverflow.com/users/1292829/arne,自定义字体xcode 4.3。这对我来说很有效,下面是我在应用程序中使用自定义字体的步骤:

Add the font to your project. I dragged and dropped the OTF (or TTF) files to a new group I created and accepted xcode's choice of copying the files over to the project folder. Create the UIAppFonts array with your fonts listed as items within the array. Just the names, not the extension (e.g. "GothamBold", "GothamBold-Italic"). Click on the project name way at the top of the Project Navigator on the left side of the screen. Click on the Build Phases tab that appears in the main area of xcode. Expand the "Copy Bundle Resources" section and click on "+" to add the font. Select the font file from the file navigator that pops open when you click on the "+". Do this for every font you have to add to the project.

在查找器和“获取信息”中找到TTF。在“全名:”的标题下,它给了我一个名字,然后我用fontWithName(我只是复制和粘贴了确切的名字,在这种情况下,没有'.ttf'扩展是必要的)。

查找ATSApplicationFontsPath

一个简单的plist条目,允许你在你的应用程序资源文件夹中包括字体文件,他们“只是工作”在你的应用程序中。

更好的解决方案是在你的信息中添加一个新的属性“应用程序提供的字体”。plist文件。

然后,你可以使用你的自定义字体像正常的UIFont。

在你现有的iOS应用程序上添加一个新的字体是非常容易的。

你只需要添加字体,如font.ttf到你的资源文件夹。

打开应用程序info.plist。添加一个新行作为“Fonts provided by application”,并输入字体名称为font.ttf。

设置字体时执行setFont:"相应字体名称"

你可以通过NSArray *check = [UIFont familyNames];来检查你的字体是否被添加。

它返回应用程序支持的所有字体。