1.添加對應的自體檔案(.ttf或.otf等)到工程目錄下,比如:SIMKAI.TTF
2.在-info.plist檔案中添加一項Fonts provided by application,item0對應的value為SIMKAI.TTF,多個自體依次添加即可。
3.使用 [UIFont fontWithName: size:]來設定自體,fontWithName的參數不一定是SIMKAI,這個字型名字需要別的方法去查。
查字型名字方法如下:
for(NSString *familyName in [UIFont familyNames]) { NSLog(@"familyName = %@", familyName); for(NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) { NSLog(@"\tfontName = %@", fontName); } }
在上面第2步添加前和添加後的情況下分別執行如上代碼,把控制台列印出的資訊分別做記錄。
控制台列印資訊樣本:
2012-08-08 14:27:56.150[1821:c07] familyName = Thonburi2012-08-08 14:27:56.153[1821:c07] fontName = Thonburi-Bold2012-08-08 14:27:56.154[1821:c07] fontName = Thonburi2012-08-08 14:27:56.154[1821:c07] familyName = Snell Roundhand2012-08-08 14:27:56.154[1821:c07] fontName = SnellRoundhand-Bold2012-08-08 14:27:56.155[1821:c07] fontName = SnellRoundhand-Black2012-08-08 14:27:56.155[1821:c07] fontName = SnellRoundhand2012-08-08 14:27:56.156[1821:c07] familyName = Academy Engraved LET2012-08-08 14:27:56.156[1821:c07] fontName = AcademyEngravedLetPlain2012-08-08 14:27:56.157[1821:c07] familyName = Marker Felt2012-08-08 14:27:56.157[1821:c07] fontName = MarkerFelt-Wide2012-08-08 14:27:56.158[1821:c07] fontName = MarkerFelt-Thin2012-08-08 14:27:56.158[1821:c07] familyName = Geeza Pro2012-08-08 14:27:56.159[1821:c07] fontName = GeezaPro-Bold2012-08-08 14:27:56.159[1821:c07] fontName = GeezaPro2012-08-08 14:27:56.182[1821:c07] familyName = Arial Rounded MT Bold2012-08-08 14:27:56.183[1821:c07] fontName = ArialRoundedMTBold2012-08-08 14:27:56.183[1821:c07] familyName = Trebuchet MS2012-08-08 14:27:56.184[1821:c07] fontName = TrebuchetMS2012-08-08 14:27:56.184[1821:c07] fontName = TrebuchetMS-Bold
用比較檔案的方法找出2次從控制台列印出的資訊的不同內容,這些不同的內容就是新添加進去的字型的名字。
從而可以看出,新添加進去的自體的名字就是 KaiTi_GB2312
設定自體的時候就應該如下操作:
[UIFont fontWithName:@"KaiTi_GB2312" size:20.0]