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


当前回答

在你现有的iOS应用程序上添加一个新的字体是非常容易的。

你只需要添加字体,如font.ttf到你的资源文件夹。

打开应用程序info.plist。添加一个新行作为“Fonts provided by application”,并输入字体名称为font.ttf。

设置字体时执行setFont:"相应字体名称"

你可以通过NSArray *check = [UIFont familyNames];来检查你的字体是否被添加。

它返回应用程序支持的所有字体。

其他回答

iOS 3.2及后续版本支持此功能。直接摘自iPhone OS 3.2新增功能文档:

自定义字体支持 想要使用自定义字体的应用程序现在可以将这些字体包含在他们的应用程序包中,并通过在他们的信息中包含UIAppFonts键向系统注册这些字体。plist文件。该键的值是一个字符串数组,用于标识应用程序bundle中的字体文件。当系统看到键时,它加载指定的字体并使它们对应用程序可用。

一旦字体设置在信息。plist,您可以使用您的自定义字体作为任何其他字体在IB或编程。

苹果开发者论坛上有一个正在进行的话题: https://devforums.apple.com/thread/37824(需要登录)

这里有一个优秀而简单的三步教程,教你如何做到这一点(断开的链接被删除)

将您的自定义字体文件添加到您的项目中,使用Xcode作为资源 为你的信息添加一个键。名为UIAppFonts的plist文件。 将该键设置为数组 对于你拥有的每种字体,输入字体文件的全名(包括扩展名)作为UIAppFonts数组的项 保存Info.plist 现在在你的应用程序中,你可以简单地调用[UIFont fontWithName:@"CustomFontName" size:12]来获得自定义字体来使用你的UILabels和UITextViews等…

另外:确保字体在复制包资源中。

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

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

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

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

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

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

编辑:这个答案在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;
}

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

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

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