今天为21世纪报系一个iPad应用做技术验证,尝试在应用中加入iPad没有的字体。先Google了一下,解决方案很简单,iOS 3.2以上都支持内嵌用户字体。介绍这个技术的blog文章或论坛帖子很多,也有中文写的,但中文的文章或帖子,几乎全是人云亦云。我猜这些可敬的作者们,多半没有自己去验证过,或者验证之后不愿把遇到的问题和解决方案分享出来。
进入正题。在iOS 3.2以上支持内嵌字体的方法,可以分为三个步骤:
1、把字体文件添加到项目的Resources文件组(千万别说你不会哦);
2、在项目的Info.plist文件中添加UIAppFonts项(类型为Array),并把字体文件名作为元素写进去;
3、在项目代码中引用字体:如 label.font = [UIFont fontWithName:@"字体名" size: 14.0f]。
简单吗?很简单。可是,如果你尝试内嵌中文字体(如Mac里面的“华文仿宋”),也许就会遇到问题。你已正确地将字体放到Resources文件组,正确地填写了Info.plist文件,而且,label.font = [UIFont fontWithName:@"华文仿宋" size:14.0f]也没有报错。好,Build and Go!怎么回事?标签文字并非如你所愿的仿宋,还是默认的Georgia字体。
这就是我说大多数中文作者都没有亲自验证的原因——或者他们都只开发英文软件?若你对字体的相关概念不熟悉,遇到这个问题太正常了。说穿了很简单,上述第三步中,必须使用字体的真实名称(family name),而不是字体文件名!
那么,“华文仿宋”的真实名称是什么?不必依赖搜索引擎,三行代码就可以找到:
在Debugger Console窗口,你将看到一大堆iPad系统支持的字体名称:
[Session started at 2010-11-03 18:06:05 +0800.]
2010-11-03 18:06:06.211 BunddleFontTest[4537:207] Bodoni Ornaments
2010-11-03 18:06:06.213 BunddleFontTest[4537:207] AppleGothic
2010-11-03 18:06:06.214 BunddleFontTest[4537:207] Gill Sans
2010-11-03 18:06:06.214 BunddleFontTest[4537:207] Hiragino Kaku Gothic ProN
2010-11-03 18:06:06.217 BunddleFontTest[4537:207] Palatino
2010-11-03 18:06:06.218 BunddleFontTest[4537:207] Copperplate
2010-11-03 18:06:06.219 BunddleFontTest[4537:207] Bodoni 72 Smallcaps
2010-11-03 18:06:06.220 BunddleFontTest[4537:207] Bradley Hand
2010-11-03 18:06:06.221 BunddleFontTest[4537:207] Heiti K
2010-11-03 18:06:06.222 BunddleFontTest[4537:207] DB LCD Temp
2010-11-03 18:06:06.222 BunddleFontTest[4537:207] Helvetica
2010-11-03 18:06:06.223 BunddleFontTest[4537:207] Marker Felt
2010-11-03 18:06:06.223 BunddleFontTest[4537:207] Times New Roman
2010-11-03 18:06:06.224 BunddleFontTest[4537:207] Verdana
2010-11-03 18:06:06.224 BunddleFontTest[4537:207] Hiragino Mincho ProN
2010-11-03 18:06:06.225 BunddleFontTest[4537:207] Zapf Dingbats
2010-11-03 18:06:06.225 BunddleFontTest[4537:207] Georgia
2010-11-03 18:06:06.225 BunddleFontTest[4537:207] Arial Rounded MT Bold
2010-11-03 18:06:06.226 BunddleFontTest[4537:207] Papyrus
2010-11-03 18:06:06.226 BunddleFontTest[4537:207] Trebuchet MS
2010-11-03 18:06:06.227 BunddleFontTest[4537:207] Baskerville
2010-11-03 18:06:06.227 BunddleFontTest[4537:207] STFangsong
2010-11-03 18:06:06.228 BunddleFontTest[4537:207] Heiti TC
2010-11-03 18:06:06.228 BunddleFontTest[4537:207] Geeza Pro
2010-11-03 18:06:06.229 BunddleFontTest[4537:207] Bodoni 72
2010-11-03 18:06:06.229 BunddleFontTest[4537:207] Courier
2010-11-03 18:06:06.229 BunddleFontTest[4537:207] Cochin
2010-11-03 18:06:06.230 BunddleFontTest[4537:207] Heiti J
2010-11-03 18:06:06.230 BunddleFontTest[4537:207] Arial
2010-11-03 18:06:06.231 BunddleFontTest[4537:207] Arial Hebrew
2010-11-03 18:06:06.232 BunddleFontTest[4537:207] Courier New
2010-11-03 18:06:06.232 BunddleFontTest[4537:207] Zapfino
2010-11-03 18:06:06.232 BunddleFontTest[4537:207] Academy Engraved LET
2010-11-03 18:06:06.233 BunddleFontTest[4537:207] Chalkduster
> >2010-11-03 18:06:06.233 BunddleFontTest[4537:207] Bodoni 72 Oldstyle
2010-11-03 18:06:06.233 BunddleFontTest[4537:207] Hoefler Text
2010-11-03 18:06:06.234 BunddleFontTest[4537:207] Futura
2010-11-03 18:06:06.234 BunddleFontTest[4537:207] Snell Roundhand
2010-11-03 18:06:06.235 BunddleFontTest[4537:207] American Typewriter
2010-11-03 18:06:06.235 BunddleFontTest[4537:207] Heiti SC
2010-11-03 18:06:06.236 BunddleFontTest[4537:207] Optima
2010-11-03 18:06:06.237 BunddleFontTest[4537:207] Helvetica Neue
2010-11-03 18:06:06.238 BunddleFontTest[4537:207] Comic Sans MS
2010-11-03 18:06:06.239 BunddleFontTest[4537:207] Party LET
2010-11-03 18:06:06.240 BunddleFontTest[4537:207] Thonburi
2010-11-03 18:06:06.241 BunddleFontTest[4537:207] Didot
你内嵌的“华文仿宋”也在其中,但它改头换面,成了“STFangsong”。没错,这就是华文仿宋的Family Name,也是代码中应该使用的字体名称。把代码改为:
labelChinese.font = [UIFont fontWithName:@"STFangsong" size:43.0f];
labelChinese.text = @"测试";
运行效果如下(我还添加了英文字体Comic Sans MS):