带有字体手提箱的 iPhone 自定义字体

人气:965 发布:2022-10-16 标签: iphone ios ios4

问题描述

我正在尝试在 iOS4 应用程序中使用 Font Suitcase 作为自定义字体,但没有成功.

我已阅读其他记录如何使用自定义字体的帖子,因此已将手提箱复制到我的项目中,将其添加到资源和 Info.plist '应用程序数组提供的字体,并尝试在 Interface Builder 和代码中使用它.但它不起作用!

我发现的所有关于自定义字体的示例都显示将它们与 ttf 或 otf 文件一起使用,但没有与手提箱一起使用.可以做到吗?如果没有,我如何将字体册中的内容放入可用作自定义字体的文件结构中?

解决方案

我遇到了同样的问题,刚刚解决了.朋友给了我几个压缩的字体,我解压缩然后文件大小为零 kb,我无法将它们安装到 Font Book 中.所以我搜索了一下,发现我的朋友应该使用 StuffIt Expander 进行压缩.于是他又把文件发给我,我用 StuffIt Expander 解压.然后我可以将字体安装到我的字体册中.

但是,这些字体在我的 iOS 应用程序中仍然不可用.所以下面是我所做的:

我发现我需要转换字体,所以我安装了Fonduhttp://fondu.sourceforge.net/安装到最后说不成功,后来还是可以用的.

然后我意识到我需要 Rosetta 来运行 Fondu,所以我查看了此页面上的说明.取出我的 Snow Leopard CD 并安装可选包http://www.macobserver.com/tmo/article/snow_leopard_installing_rosetta/p>

打开终端并转到您的字体所在的目录.键入命令:火锅 [字体文件]然后你会得到一个提示,询问你是否要保存[字体文件].pfb选择是.

现在在您的 XCode 中,将 pfb 文件添加到您的项目中.然后转到 info.plist 并将文件添加到 UIAppFonts(包括 .pfb 扩展名).

我在 UITableViewCell 中测试了字体

cell.textLabel.font = [UIFont fontWithName:@"[字体文件]" size:30];//不包含 .pfb 扩展名

字体显示正常.我最初认为 iOS 需要一个 .ttf 文件,但看起来 .pfb 也可以.

希望这会有所帮助.

其他测试提示:如上所示添加 UIAppFonts plist 数组后,在 appDidFinishLaunchingWithOptions 方法和控制台类型中添加断点

po [UIFont familyNames]

这将为您提供已安装字体的列表,因此请搜索您的字体系列名称.将其复制并粘贴到此:

po [UIFont fontNamesForFamilyName:@"familyname"]

这将为您获取您在代码中引用的字体名称.我没有尝试过 [font filename] 方法,但这为您提供了应用程序用来引用您想要的确切字体的名称.当字体包含一系列变体或您从同一个系列安装了多个变体时,这一点很重要.例如 helvetica 有四种变体,因此您可以通过这种方式选择正确的一种:

lblMyLabel.font = [UIFont fontWithName:@"fontname" size:lblMyLabel.font.pointSize]

例如

lblMyLabel.font = [UIFont fontWithName:@"Helvetica-Oblique" size:lblMyLabel.font.pointSize]

这会保留您最初用于标签的字体磅值.我也只有在视图加载后才成功.

I am attempting to use a Font Suitcase for a custom font in an iOS4 app but am not having any success.

I have read other posts that have documented how to use custom fonts, so have copied the suitcase into my project, added it to resources and the Info.plist ' Fonts provided by application' array, and attempted to use it both in Interface Builder and in code. But it doesn't work!

All examples I've found regarding custom fonts show using them with ttf or otf files, but nothing with a suitcase. Can it be done? If not, how do I get something out of the Font Book into a file structure that can be used as a custom font?

解决方案

I had the same problem and just solved it. Friend gave me a couple of fonts zipped, I unzipped and then the file sizes are zero kb and I couldn't install them into Font Book. So I googled and found out that my friend should have used StuffIt Expander to zip. So he sent me the file again, and I used StuffIt Expander to unzip. I was then able to install the fonts into my Font Book.

However, the fonts are still not usable in my iOS app. So below is what I did:

I found out I need to convert the font, so I installed Fondu http://fondu.sourceforge.net/ The installation ended up saying not successful, but later I was still able to use.

Then I realise I need Rosetta to run Fondu, so I looked at the instructions on this page. Got my Snow Leopard CD out and install optional package http://www.macobserver.com/tmo/article/snow_leopard_installing_rosetta/

Open Terminal and go to the directory where your font is. Type the command: fondu [font file] Then you will get a prompt asking if you want to save [font file].pfb Choose yes.

Now in your XCode, add the pfb file into your project. Then go to info.plist and add the file to UIAppFonts (include the .pfb extension).

I tested the font in a UITableViewCell

cell.textLabel.font = [UIFont fontWithName:@"[font file]" size:30]; // do not include .pfb extension

And the font is showing up fine. I initially thought iOS will require a .ttf file but looks like .pfb is fine also.

Hope this helps.

Additional testing tips: Once you add the UIAppFonts plist array as shown above add a breakpoint in your appDidFinishLaunchingWithOptions method and at the console type

po [UIFont familyNames]

That gets you a list of installed fonts so search for your font family name. Copy that and paste it into this:

po [UIFont fontNamesForFamilyName:@"familyname"]

This gets you the font name you reference in your code. I've not tried the [font filename] approach but this gives you the name the app uses to reference the exact font you want. This is important when a font contains a series of variants or you have multiple installed from the same family. For example helvetica has four variants so you can pick the right one this way:

lblMyLabel.font = [UIFont fontWithName:@"fontname" size:lblMyLabel.font.pointSize]

E.g.

lblMyLabel.font = [UIFont fontWithName:@"Helvetica-Oblique" size:lblMyLabel.font.pointSize]

This keeps the font point size you used originally for the label. I've also only had success once the view is loaded.

837