~Onlooker 2008-12-11 20:21 采纳率: 0%
浏览 406
已采纳

我可以在 iPhone 应用程序中嵌入自定义字体吗?

This question relates to iOS pre-3.2. As of 3.2, this functionality is easily achievable using samvermette's answer below, and I have changed the Accepted Answer (from command to samvermette) to reflect this. I can't give credit to both answers (besides upvotes) but they are both good.

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?

I found below links:

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.


EDIT - failed -[UIFont fontWithName:size:] experiment

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];

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


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.


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.

转载于:https://stackoverflow.com/questions/360751/can-i-embed-a-custom-font-in-an-iphone-application

  • 写回答

29条回答 默认 最新

  • 7*4 2010-04-11 05:01
    关注

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

    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.

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

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

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

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(28条)

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图