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


当前回答

作为一个增强@bdev的答案,这里是一个更新版本列出自定义字体。

步骤1:使用@bdev的答案找出所有系统字体并保存到文件中。

将以下代码放在第一个视图控制器的-(void)viewDidLoad中,在[super viewDidLoad]之后(或在App Delegate中):

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES);
NSMutableArray *system_fonts = [NSMutableArray array];
for (NSString *familyName in [UIFont familyNames]) {
    for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
        [system_fonts addObject:fontName];
    }
}
if([paths count] > 0) {
    [system_fonts writeToFile:[[paths objectAtIndex:0]
                               stringByAppendingPathComponent:@"array.out"] atomically:YES];
}

运行应用程序一次。事后停止。

步骤2:添加自定义字体到项目

使用已接受的答案中显示的方法,添加您的自定义字体(记得更新.plist并通过勾选add to Target添加要构建的字体文件。

步骤3:将系统字体与当前字体列表进行比较

将步骤1中的代码替换为:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES);
NSMutableArray *system_fonts = [NSMutableArray arrayWithContentsOfFile:[[paths objectAtIndex:0]
                                                                        stringByAppendingPathComponent:@"array.out"]];

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

运行App,你添加的自定义字体列表就会显示出来。

这适用于iOS 3.2到iOS 6(未来的版本可能会正常工作)。也适用于.ttc和.ttf。

其他回答

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

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

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

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

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

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

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

对于iOS 3.2及以上版本: 使用上面几个提供的方法,它们是:

将您的字体文件(例如,chalkdust .ttf)添加到XCode项目的资源文件夹中。 开放的信息。plist并添加一个名为UIAppFonts的新键。这个键的类型应该是array。 将您的自定义字体名称添加到该数组,包括扩展名(" chalkdust .ttf")。 在你的应用中使用[UIFont fontWithName:@"真实字体名称"大小:16]。

但 “真正的字体名称”并不总是一个你看到的字体。最好的方法是问你的设备它看到的字体和确切的名称是什么。

我使用uifont-name-grabber发布在: uifont-name-grabber

只需将你想要的字体放到xcode项目中,将文件名添加到它的plist中,并在你要构建的设备上运行它,它会通过电子邮件给你一个完整的字体列表,使用UIFont fontWithName:所期望的名称。

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

目前还没有发布,但下一个版本的cocos2d (2d游戏框架)将支持可变长度的位图字体作为角色地图。

http://code.google.com/p/cocos2d-iphone/issues/detail?id=317

作者没有确定这个版本的发布日期,但我确实看到一个帖子说它将在未来一两个月发布。