我可以在iPhone应用程序中嵌入自定义字体吗? [英] Can I embed a custom font in an iPhone application?

查看:129
本文介绍了我可以在iPhone应用程序中嵌入自定义字体吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与iOS 3.2之前相关。从3.2这个功能是容易实现使用samvermette的答案下面,我已经改变了接受的答案(从commanda到samvermette)以反映这一点。

我想要一个应用程式包含自订字型渲染文本,加载它,然后使用标准的 UIKit 元素,如 UILabel 。这是可能的吗?

I would like to have an app include a custom font for rendering text, load it, and then use it with standard UIKit elements like UILabel. Is this possible?

我找到了以下链接:

  • http://discussions.apple.com/thread.jspa?messageID=8304744
  • http://forums.macrumors.com/showthread.php?t=569311

但这些都需要我自己渲染每个字形,这有点太努力工作,特别是对于多行文本。

but these would require me to render each glyph myself, which is a bit too much like hard work, especially for multi-line text.

我也发现了一些帖子,直接说,这是不可能的,但没有正当性,所以我在寻找

I've also found posts that say straight out that it's not possible, but without justification, so I'm looking for a definitive answer.

我下载了Harrowprint.tff(从这里),并将其添加到我的资源目录和项目。然后我尝试这个代码:

I downloaded Harrowprint.tff (downloaded from here) and added it to my Resources directory and to the project. I then tried this code:

UIFont* font = [UIFont fontWithName:@"Harrowprint" size:20];

导致抛出异常。查看Finder中的TTF文件确认字体名称为Harrowprint。

which resulted in an exception being thrown. Looking at the TTF file in Finder confirmed that the font name was Harrowprint.

EDIT - 目前已有多个回复,关于X或Y的文档。我已经广泛地实验了所有这些,并没有地方。在一种情况下,X证明是仅与OS X相关,而不是在iPhone上。因此,我为这个问题设置赏金,我将奖励给第一个提供答案(只使用记录的API)的人,他回答充分的信息,使它在设备上工作。在模拟器上工作将是一个奖金。

EDIT - there have been a number of replies so far which tell me to read the documentation on X or Y. I've experimented extensively with all of these, and got nowhere. In one case, X turned out to be relevant only on OS X, not on iPhone. Consequently I am setting a bounty for this question, and I will award the bounty to the first person who provides an answer (using only documented APIs) who responds with sufficient information to get this working on the device. Working on the simulator too would be a bonus.

编辑 - 看起来赏金会自动奖励最高数字的答案的选票。有趣。没有人真正提供一个解决问题的答案 - 涉及编码自己的 UILabel 子类的解决方案不支持单词换行,这是我的一个基本功能 - 虽然我想我可以延长它这样做。

EDIT - It appears that the bounty auto-awards to the answer with the highest number of votes. Interesting. No one actually provided an answer that solved the question as asked - the solution that involves coding your own UILabel subclass doesn't support word-wrap, which is an essential feature for me - though I guess I could extend it to do so.

推荐答案

iOS 3.2及更高版本支持此功能。直接从iPhone OS 3.2中的新功能 doc:

iOS 3.2 and later support this. Straight from the What's New in iPhone OS 3.2 doc:


自定义字体支持

要使用自定义字体的应用程序现在可以在其应用程序包中包含这些字体,并通过在其Info.plist文件中包含UIAppFonts键在系统中注册这些字体。此键的值是一个字符串数组,用于标识应用程序包中的字体文件。当系统看到该键时,它会加载指定的字体并使其可用于应用程序。

Custom Font Support
Applications that want to use custom fonts can now include those fonts in their application bundle and register those fonts with the system by including the UIAppFonts key in their Info.plist file. The value of this key is an array of strings identifying the font files in the application’s bundle. When the system sees the key, it loads the specified fonts and makes them available to the application.

Info.plist ,您可以使用自定义字体作为IB中的任何其他字体或编程。

Once the fonts have been set in the Info.plist, you can use your custom fonts as any other font in IB or programatically.

Apple开发者论坛上持续关注的线程:

https://devforums.apple.com / thread / 37824 (需要登录)

There is an ongoing thread on Apple Developer Forums:
https://devforums.apple.com/thread/37824 (login required)

这里有一个非常简单的3步教程,介绍如何实现这一点

And here's an excellent and simple 3 steps tutorial on how to achieve this (broken link removed)


  1. 使用Xcode作为资源将自定义字体文件添加到项目中

  2. Info.plist 文件 UIAppFonts

  3. 将此键设为数组

  4. 对于每种字体,输入字体文件的全名(包括扩展名)作为 UIAppFonts 数组的项目

  5. 保存 Info.plist

  6. 现在在您的应用程序中,您只需调用 [UIFont font -ithName:@CustomFontNamesize:12] 以获取要与 UILabels UITextViews
  1. Add your custom font files into your project using Xcode as a resource
  2. Add a key to your Info.plist file called UIAppFonts.
  3. Make this key an array
  4. For each font you have, enter the full name of your font file (including the extension) as items to the UIAppFonts array
  5. Save Info.plist
  6. Now in your application you can simply call [UIFont fontWithName:@"CustomFontName" size:12] to get the custom font to use with your UILabels and UITextViews, etc…

此外:请确保字体在您的复制捆绑资源中。

Also: Make sure the fonts are in your Copy Bundle Resources.

这篇关于我可以在iPhone应用程序中嵌入自定义字体吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆