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


当前回答

也许作者忘记给字体起一个Mac喜欢的名字了?

在FontForge中打开字体,然后进入元素>字体信息 有一个“Mac”选项,你可以设置喜爱的名称。 在文件>导出字体下,您可以创建一个新的ttf

你也可以试试导出对话框中的“Apple”选项。

免责声明:我不是IPhone开发者!

其他回答

这是如何做这件事的一步一步的说明。不需要额外的库或任何特殊的编码。

http://shang-liang.com/blog/custom-fonts-in-ios4/

大多数时候问题在于字体而不是方法。最好的方法是使用一种肯定有效的字体,比如verdana或geogia。然后更改为想要的字体。如果它不起作用,可能是字体名称不正确,或者字体格式不正确。

如果你使用的是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.

在信息。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]];

我在iOS 3.1.2上尝试了这个页面上的各种建议,以下是我的结论:

简单地使用[UIFont fontWithName:size:]与资源目录中的字体将不起作用,即使使用FontForge设置了FOND名称。

[UIFont fontWithName:size:]将工作,如果字体首先使用GSFontAddFromFile加载。但是GSFontAddFromFile不是iOS 3.1.2的一部分,所以它必须像@rpetrich所描述的那样动态加载。

一个重要的注意事项:您应该使用与字体相关的“PostScript名称”,而不是其全名或家族名称。此名称通常与字体的正常名称不同。