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


当前回答

首先将.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.

您可以在资源文件夹中添加所需的“FONT”文件。然后转到项目信息。plist文件,并使用键“Fonts provided by the application”和值“FONT NAME”。

然后你可以调用方法[UIFont fontwithName:@"FONT NAME" size:12];

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

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

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

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

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

在信息。plist添加“Fonts provided by application”条目,并以字符串的形式包含字体名称:

Fonts provided by application
           Item 0        myfontname.ttf
           Item 1        myfontname-bold.ttf
           ...

然后运行以下命令,确保包含了你的字体:

for (NSString *familyName in [UIFont familyNames]) {
    for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
         NSLog(@"%@", fontName);
    }
}

请注意,您的ttf文件名可能与您设置标签字体时使用的名称不同(您可以使用上面的代码来获得"fontWithName"参数):

[label setFont:[UIFont fontWithName:@"MyFontName-Regular" size:18]];

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

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

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