1.将需要的字体库xxx.ttf添加到工程中,并且一定要在copy bundle resources中添加。不添加是没有效果的。
2.在info.plist 文件中添加 fonts provided by application 默认为array,也可以使用dictionary 添加value为 xxx.ttf
可以添加多个,使用的时候写对应字体名字就行。(中英文都无所谓的。)
如下图:
3.在你的工程就可以直接用了。xx.font = [UIFont fontWithName:@"FZZhunYuan-M02S" size:20.0];(注意:这里的字体名字为familyName,不是文件名)通过遍历字体,可以得到所添加字体的familyName,应该是在数组的最后面。关于这个名字的fontName是多少,可以通过下面的方法进行寻找。只要对比之前没有添加的字体库的名字和添加过的字体库的名字,就可以轻而易举找到想要的fontName了。
NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
NSArray *fontNames;
NSInteger indFamily, indFont;
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]);
}
}
NSArray *fontNames;
NSInteger indFamily, indFont;
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]);
}
}
PS: 在这个网址上http://font.knowsky.com/fonts_150_7.htm有很多字体。大家可以选择想用的。