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


当前回答

编辑:这个答案在iOS3.2失效;使用UIAppFonts

我能够成功加载自定义UIFonts的唯一方法是通过私有的GraphicsServices框架。

下面将加载应用程序主包中的所有.ttf字体:

BOOL GSFontAddFromFile(const char * path);
NSUInteger loadFonts()
{
    NSUInteger newFontCount = 0;
    for (NSString *fontFile in [[NSBundle mainBundle] pathsForResourcesOfType:@"ttf" inDirectory:nil])
        newFontCount += GSFontAddFromFile([fontFile UTF8String]);
    return newFontCount;
}

一旦字体被加载,它们就可以像apple提供的字体一样使用:

NSLog(@"Available Font Families: %@", [UIFont familyNames]);
[label setFont:[UIFont fontWithName:@"Consolas" size:20.0f]];

GraphicsServices甚至可以在运行时加载,以防API在未来消失:

#import <dlfcn.h>
NSUInteger loadFonts()
{
    NSUInteger newFontCount = 0;
    NSBundle *frameworkBundle = [NSBundle bundleWithIdentifier:@"com.apple.GraphicsServices"];
    const char *frameworkPath = [[frameworkBundle executablePath] UTF8String];
    if (frameworkPath) {
        void *graphicsServices = dlopen(frameworkPath, RTLD_NOLOAD | RTLD_LAZY);
        if (graphicsServices) {
            BOOL (*GSFontAddFromFile)(const char *) = dlsym(graphicsServices, "GSFontAddFromFile");
            if (GSFontAddFromFile)
                for (NSString *fontFile in [[NSBundle mainBundle] pathsForResourcesOfType:@"ttf" inDirectory:nil])
                    newFontCount += GSFontAddFromFile([fontFile UTF8String]);
        }
    }
    return newFontCount;
}

其他回答

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

正如之前所有的答案所表明的,这是非常有可能的,而且在更新的iOS版本中非常容易。

我知道这不是一个技术上的答案,但因为很多人都做错了(这实际上违反了许可,如果你被起诉,可能会花费你很多钱),让我在这里强调一个警告:在iOS(或任何其他类型)应用程序中嵌入自定义字体基本上是重新分配字体。大多数商业字体的许可都禁止再分发,所以请确保您按照许可行事。

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

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

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

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

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

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

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

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

从iOS 4.1开始,有了一种使用自定义字体的新方法。它允许你动态加载字体,从文件中包含的应用程序,下载的数据,或你有什么。它还可以让你在需要的时候加载字体,而旧的方法是在应用启动时加载它们,如果你有很多字体,这可能会花费很长时间。

新方法在ios-dynamic-font-loading中描述

使用CTFontManagerRegisterGraphicsFont函数,给它一个带有字体数据的缓冲区。然后它就可以用于UIFont和web视图,就像旧的方法一样。下面是该链接的示例代码:

NSData *inData = /* your font-file data */;
CFErrorRef error;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)inData);
CGFontRef font = CGFontCreateWithDataProvider(provider);
if (! CTFontManagerRegisterGraphicsFont(font, &error)) {
    CFStringRef errorDescription = CFErrorCopyDescription(error)
    NSLog(@"Failed to load font: %@", errorDescription);
    CFRelease(errorDescription);
}
CFRelease(font);
CFRelease(provider);