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];