1、确定你的项目工程的resouce下有你要用的字体文件(.ttf,.odf)。
2、然后在你的工程的Info.plist文件中新建一行(Add Row),添加key为:UIAppFonts(在我的机子上尝试了,它会自动转换成Fonts provided by application),类型为Array或Dictionary都行;添加Value为XXX.ttf(你字体的名字)。忘说了,是在建立的UIAppFonts再建一对键值对,key为Item 0,值为xxxx.ttf。是这样,可以添加多个,使用的时候写对应字体名字就行。
3、
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]);
}
[fontNames release];
}
[familyNames release];
在程序中先加入这段代码,运行,查看console,以上程式会列出所有的字型,当然也包含UIAPPFonts所加的字型,但请注意,名字可能差距很大,要自己找一下
例:
msjh.ttf (Window7中的微软正黑体) , 加入UIAPPFonts
执行以上程式会列出
Family name: Microsoft JhengHei
Font name: MicrosoftJhengHeiRegular
要使用字体的Family name,而不是字体的文件名,弄错了将无法看到效果
在你的项目里要用字体的时候 xx.font = [UIFont fontWithName:@"Microsoft JhengHei" size:20.0],这样就可以了。
NSArray *familyName = [[NSArrayalloc]initWithArray:[UIFontfamilyNames]];
NSLog(@"f---------%@",familyName);
NSArray *fontNames;
NSInteger indFamily,indFont;
for (indFamily=0; indFamily < [familyNamecount]; ++indFamily) {
NSLog(@"Family name--------%@",[familyNameobjectAtIndex:indFamily]);
fontNames = [[NSArray alloc]initWithArray:[UIFontfontNamesForFamilyName:[familyName objectAtIndex:indFamily]]];
for (indFont =0; indFont < [fontNamescount]; ++indFont) {
NSLog(@"font name----%@",[fontNamesobjectAtIndex:indFont]);
}
[fontNames release];
}
[familyName release];
//UniDreamLED----------UniDream_LED
UILabel *labelFont = [[UILabelalloc]initWithFrame:CGRectMake(30,100, 160, 50)];
labelFont.backgroundColor = [UIColorclearColor];
labelFont.textColor = [UIColorblackColor];
labelFont.text=@"123456";
labelFont.font = [UIFontfontWithName:@"UniDreamLED" size:26.0];
[self.view addSubview:labelFont];
[labelFont release];