iOS開發 字體適配

今天蛋疼,突然想用下漂亮的字體,就稍微用研究了一下這個用法,很簡單,就三步吧

1.第一步找到你想用的字體的 ttf 格式。加入到你的工程的resouce目錄下。


2.在工程的plist中AddRow,“Fonts provided by application” ,然後添加key爲item0,value爲你剛纔加入的testFont.ttf 。

是這樣,可以添加多個,使用的時候寫對應字體名字就行。

3.在你的工程就可以直接用了。xx.font = [UIFont fontWithName:@"testFont" size:20.0];


注意:

在程序中先加入這段代碼,運行,

[html] view plaincopy
  1. NSArray *familyNames =[[NSArray alloc]initWithArray:[UIFont familyNames]];  
  2.    NSArray *fontNames;  
  3.    NSInteger indFamily, indFont;  
  4.    NSLog(@"[familyNames count]===%d",[familyNames count]);  
  5.    for(indFamily=0;indFamily<[familyNames count];++indFamily)  
  6.          
  7. {  
  8.     NSLog(@"Family name: %@", [familyNames objectAtIndex:indFamily]);  
  9.        fontNames =[[NSArray alloc]initWithArray:[UIFont fontNamesForFamilyName:[familyNames objectAtIndex:indFamily]]];  
  10.   
  11.     for(indFont=0; indFont<[fontNames count]; ++indFont)  
  12.              
  13.     {  
  14.         NSLog(@"Font name: %@",[fontNames objectAtIndex:indFont]);  
  15.              
  16.        }  
  17.          
  18.     [fontNames release];  
  19. }  
  20.      
  21. [familyNames release];  


查看console,以上程式會列出所有的字型,當然也包含“Fonts provided by application”所加的字型,但請注意,名字可能差距很大,要自己找一下
例:
      testFont.ttf   , 加入Fonts provided by application
       執行以上程式會列出

[html] view plaincopy
  1. 2012-10-20 21:56:21.321 MyAnimationTest[5397:c07]  Family name: HAKUYOGuiFanZi3500  
  2. 2012-10-20 21:56:21.321 MyAnimationTest[5397:c07]     Font name: HAKUYOGuiFanZi3500  
  3. 2012-10-20 21:56:21.322 MyAnimationTest[5397:c07] Family name: Didot  
  4. 2012-10-20 21:56:21.322 MyAnimationTest[5397:c07]     Font name: Didot-Italic  
  5. 2012-10-20 21:56:21.322 MyAnimationTest[5397:c07]     Font name: Didot  
  6. 2012-10-20 21:56:21.323 MyAnimationTest[5397:c07]     Font name: Didot-Bold  
  7. 2012-10-20 21:56:21.323 MyAnimationTest[5397:c07] Family name: Bodoni 72 Smallcaps  
  8. 2012-10-20 21:56:21.323 MyAnimationTest[5397:c07]     Font name: BodoniSvtyTwoSCITCTT-Book  
要使用字體的Family name,而不是字體的文件名,弄錯了將無法看到效果。

注意還有一個重要的是: 在“Copy Boundle Resources”中加入剛纔添加的字體文件xxx.ttf ,如下:



最好直接使用就ok了。


效果 :


你可以在http://www.webpagepublicity.com/free-fonts.html下載更多ttf字體。 

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