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


当前回答

在iOS 4中有一种使用自定义字体的简单方法。

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

不幸的是,IB不允许使用自定义字体初始化标签。看这个问题来解决这个问题。我最喜欢的解决方案是使用自定义UILabel子类:

@implementation CustomFontLabel

- (id)initWithCoder:(NSCoder *)decoder
{
    if (self = [super initWithCoder: decoder])
    {
        [self setFont: [UIFont fontWithName: @"Chalkduster" size: self.font.pointSize]];
    }
    return self;
}

@end

其他回答

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

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

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

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

更好的解决方案是在你的信息中添加一个新的属性“应用程序提供的字体”。plist文件。

然后,你可以使用你的自定义字体像正常的UIFont。

是的,你可以在你的应用程序中使用自定义字体

按照下面的步骤:

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

应用这个后,如果你没有得到正确的字体 然后双击自定义字体,仔细看顶部字体的名字是来的 然后复制这个字体,粘贴到这里[UIFont fontWithName:@" here past your Custom fonts Name" size:20] 我希望你能得到正确答案

查找ATSApplicationFontsPath

一个简单的plist条目,允许你在你的应用程序资源文件夹中包括字体文件,他们“只是工作”在你的应用程序中。