iOS开发中经常会修改字体样式,设置成自己想要的字体,总结了一下方法:
1.在程序中先加入这段代码,运行:
- NSArray *familyNames =[[NSArray alloc]initWithArray:[UIFont familyNames]];
- NSArray *fontNames;
- NSInteger indFamily, indFont;
- NSLog(@"[familyNames count]===%d",[familyNames count]);
- for(indFamily=0;indFamily<[familyNames count];++indFamily)
- {
- NSLog(@"Family name: %@", [familyNames objectAtIndex:indFamily]);
- fontNames =[[NSArray alloc]initWithArray:[UIFont fontNamesForFamilyName:[familyNames objectAtIndex:indFamily]]];
- for(indFont=0; indFont<[fontNames count]; ++indFont)
- {
- NSLog(@"Font name: %@",[fontNames objectAtIndex:indFont]);
- }
- [fontNames release];
- }
- [familyNames release];
将打印出的运行结果,保存到一个.txt文件中,为的是记录下添加新字体前系统原生的字体有哪些。
2.将自己想用的字体(ttf格式或者ttc格式均可)加入到工程Supporting Files目录下。
3.设置plist文件
在plist文件中添加一个Row,“Fonts provided by application”,添加然后添加key为item0,value为你刚才加入的XXX.ttf 。
4.重新运行程序,执行步骤1的代码,将打印的运行结果保存在另一个.txt文档中,记录下添加新字体后的字体。
5.推荐使用beyond compare这款软件来对比两个.txt文档的区别,找出新添加的字体名,因为新添加的字体名和我们添加的名称会不一致。附上软件的下载网址http://www.pc6.com/mac/118063.html
6.这样就可以在工程中使用新字体了,x x.font = [UIFont fontWithName:@"XXX" size:20.0];