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


当前回答

首先将.odt格式的字体添加到资源中,在本例中我们将使用DINEngschriftStd。Otf,然后使用这段代码将字体分配给标签

[theUILabel setFont:[UIFont fontWithName:@"DINEngschriftStd" size:21]];

要确保你的字体被加载到项目中,只需调用

NSLog(@"Available Font Families: %@", [UIFont familyNames]);

在.plist中,必须声明字体。只需添加一个'由应用程序提供的字体'记录,并添加一个带有字体名称的item 0字符串(DINEngschriftStd.otf)

其他回答

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

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

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

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

我做了一切可能,但新的字体不出现,所以我找到了解决方案:

当你拖动fot文件(otf或ttf)时,不要忘记勾选“添加到目标”下的复选框。

这样做之后,你的字体就会出现,一切都会正常工作。

首先将.odt格式的字体添加到资源中,在本例中我们将使用DINEngschriftStd。Otf,然后使用这段代码将字体分配给标签

[theUILabel setFont:[UIFont fontWithName:@"DINEngschriftStd" size:21]];

要确保你的字体被加载到项目中,只需调用

NSLog(@"Available Font Families: %@", [UIFont familyNames]);

在.plist中,必须声明字体。只需添加一个'由应用程序提供的字体'记录,并添加一个带有字体名称的item 0字符串(DINEngschriftStd.otf)

如果你使用的是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'扩展是必要的)。