在iphone中使用自定義字體

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],這樣就可以了。

 

原文地址http://blog.csdn.net/sjzsp/archive/2011/04/21/6338282.aspx

 

在使用過程中添加Gill Sans MT字體,然後反覆調用Gill Sans MT-Bold未果,打印項目中所有字體後發現應該使用GillSansMT-Bold,大部分字體使用Bold時都將前面字體名中的空格去掉或者有所改動,大家要注意呀~免得像我一樣走彎路了~!

Family name: Gill Sans MT
     Font name: GillSansMT
     Font name: GillSansMT-Bold
     Font name: GillSansMT-Italic

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章