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 = Thonburi
- 2012-08-08 14:27:56.153[1821:c07] fontName = Thonburi-Bold
- 2012-08-08 14:27:56.154[1821:c07] fontName = Thonburi
- 2012-08-08 14:27:56.154[1821:c07] familyName = Snell Roundhand
- 2012-08-08 14:27:56.154[1821:c07] fontName = SnellRoundhand-Bold
- 2012-08-08 14:27:56.155[1821:c07] fontName = SnellRoundhand-Black
- 2012-08-08 14:27:56.155[1821:c07] fontName = SnellRoundhand
- 2012-08-08 14:27:56.156[1821:c07] familyName = Academy Engraved LET
- 2012-08-08 14:27:56.156[1821:c07] fontName = AcademyEngravedLetPlain
- 2012-08-08 14:27:56.157[1821:c07] familyName = Marker Felt
- 2012-08-08 14:27:56.157[1821:c07] fontName = MarkerFelt-Wide
- 2012-08-08 14:27:56.158[1821:c07] fontName = MarkerFelt-Thin
- 2012-08-08 14:27:56.158[1821:c07] familyName = Geeza Pro
- 2012-08-08 14:27:56.159[1821:c07] fontName = GeezaPro-Bold
- 2012-08-08 14:27:56.159[1821:c07] fontName = GeezaPro
- 2012-08-08 14:27:56.182[1821:c07] familyName = Arial Rounded MT Bold
- 2012-08-08 14:27:56.183[1821:c07] fontName = ArialRoundedMTBold
- 2012-08-08 14:27:56.183[1821:c07] familyName = Trebuchet MS
- 2012-08-08 14:27:56.184[1821:c07] fontName = TrebuchetMS
- 2012-08-08 14:27:56.184[1821:c07] fontName = TrebuchetMS-Bold
用比較文件的方法找出2次從控制台打印出的信息的不同內容,這些不同的內容就是新添加進去的字體的名字。
從而可以看出,新添加進去的自體的名字就是 KaiTi_GB2312
設置自體的時候就應該如下操作:
- [UIFont fontWithName:@"KaiTi_GB2312" size:20.0]