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


当前回答

Swift,代码方式:(也适用于Swift 2.0)

添加所需的字体到你的项目(就像添加图像,只需拖动到Xcode),确保他们是针对你的项目 添加这个方法并加载自定义字体(推荐在appDelegate didFinishLaunchingWithOptions中)

func loadFont(filePath: String) {

    let fontData = NSData(contentsOfFile: filePath)!

    let dataProvider = CGDataProviderCreateWithCFData(fontData)
    let cgFont = CGFontCreateWithDataProvider(dataProvider)!

    var error: Unmanaged<CFError>?
    if !CTFontManagerRegisterGraphicsFont(cgFont, &error) {
        let errorDescription: CFStringRef = CFErrorCopyDescription(error!.takeUnretainedValue())
        print("Unable to load font: %@", errorDescription, terminator: "")
    }

}

使用的例子:

if let fontPath = NSBundle.mainBundle().pathForResource("My-Font", ofType: "ttf"){
      loadFont(fontPath)
}

使用字体:

UIFont(name: "My-Font", size: 16.5)

其他回答

从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);

是的,您可以包括自定义字体。参考UIFont的文档,特别是fontWithName:size:方法。

1)确保在你的资源文件夹中包含了该字体。

2)字体的“名称”不一定是文件名。

3)确保你有使用这种字体的合法权利。通过把它包含在你的应用中,你也在传播它,你需要有这样做的权利。

在iOS 8+和Xcode 6+中,你可以很容易地做到这一点。以下是步骤:

1)拖拽你的字体到Xcode支持文件文件夹。不要忘记在添加到目标部分标记你的应用程序。从这一刻起,你可以在IB中使用这种字体,并从字体托盘中选择它。

2)要使此字体在您的设备中可用,请打开您的信息。plist和添加由应用程序键提供的字体。它将包含Item 0键,您必须添加您的字体名称作为值。字体名称可以与您的字体文件名不同。但首先,在大多数情况下,尝试添加您的文件名。

如果没有,这篇文章总是帮助我。

下面是本文中的代码片段,可以帮助您找到字体名称。

func allFonts(){

   for family in UIFont.familyNames(){

       println(family)


       for name in UIFont.fontNamesForFamilyName(family.description)
       {
           println("  \(name)")
       }

   }

}

EDIT

我想说的是,你需要将字体文件添加到你的目标构建阶段,复制捆绑资源。没有它,你将看不到你的字体在设备上。这可能会导致意想不到的行为。

例如,我遇到了一个错误,当UITextField有自定义字体,但这种字体不在复制Bundle资源。当我用这个文本框segue到视图控制器时,在viewDidLoad函数被调用之前有大约4秒的延迟。解决字体问题消除了这种延迟。所以,建议检查两遍。(rdar://20028250) Btw,我无法重现错误,但我确信问题是字体。

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

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

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

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

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