ITeye博客

iOS使用自定义字体

janedoneway · 2012-11-27 12:11 · iPhone · 移动开发 · Markdown

From: http://blog.csdn.net/csy172775922/article/details/7618462

 

1、向工程内添加*.tff字体文件。

2、检查*.tff字体文件是否包含在Target -> Bundle Phases -> Copy Bound Resources 下的列表中没有就添加进来。

3、编辑工程的Info.plist在其中添加Fonts provided by application(Raw Keys:UIAppFonts)并在Item中添加*.tff。

4、获取字体名称

查找字体名通过代码找出非系统字体

[cpp] view plain copy

  1. //查找字体名
  2. NSArray *array = [UIFont familyNames];
  3. for  (NSString * familyname in array) {
  4. NSLog(@"Family:%@" ,familyname);
  5. NSArray *fontnames = [UIFont fontNamesForFamilyName:familyname];
  6. for  (NSString *name in fontnames) {
  7. NSLog(@"Font Name:%@" ,name);
  8. }
  9. }

查找字体名称通过PC

直接在Mac下打开tff字体文件即可查看。

5、按照字体名加载字体 [UIFont fontWithName:(NSString *) size:(CGFloat)]。

 

 

From: http://mobilesolutions.blog.163.com/blog/static/189224176201212352512275/

 

 

 

众说周知,在iOS系统提供的字体是有限的,我们可以利用UIFont 类取出查看iOS系统支持的所有字体类型。

在此以UITableView列表来展示iPhone支持的所有字体类型。

-



 



(



NSInteger



)



numberOfSectionsInTableView



:(



UITableView



 



*)



tableView




{







    



//字体家族总数






    



return



 



[[



UIFont



 familyNames



]



 count



];



 




}





-



 



(



NSInteger



)



tableView



:(



UITableView



 



*)



tableView numberOfRowsInSection



:(



NSInteger



)



section




{







    



//字体家族包括的字体库总数




    



return



 



[[



UIFont



 fontNamesForFamilyName



:[[



UIFont



 familyNames



]



 objectAtIndex



:



section



]



 



]



 count



];




}





-



 



(



NSString



 



*)



tableView



:(



UITableView



 



*)



tableView titleForHeaderInSection



:(



NSInteger



)



section




{




    



//字体家族名称




    



return



 



[[



UIFont



 familyNames



]



 objectAtIndex



:



section



];




}






 



-



 



(



NSInteger



)



tableView



:(



UITableView



 



*)



tableView sectionForSectionIndexTitle



:(



NSString



 



*)



title atIndex



:(



NSInteger



)



index




{




    



[



tableView scrollToRowAtIndexPath



:[



NSIndexPath



 indexPathForRow



:



0



 inSection



:



index



]



 atScrollPosition



:



UITableViewScrollPositionMiddle



 animated



:



NO



];




    



return



 index



;




}







 



-



 



(



UITableViewCell



 



*)



tableView



:(



UITableView



 



*)



tableView cellForRowAtIndexPath



:(



NSIndexPath



 



*)



indexPath







{







    



static



 



NSString



 



*



CellIdentifier



 



=



 



@



"Cell"



;




    



UITableViewCell



 



*



cell 



=



 



[



tableView dequeueReusableCellWithIdentifier



:



CellIdentifier



];




    



if



 



(



cell 



==



 



nil



)



 



{




        cell 



=



 



[[[



UITableViewCell



 alloc



]



 initWithStyle



:



UITableViewCellStyleDefault



 reuseIdentifier



:



CellIdentifier



]



 autorelease



];




        cell



.



accessoryType 



=



 



UITableViewCellAccessoryDisclosureIndicator



;




    



}








    



// Configure the cell.




    cell



.



textLabel



.



textColor 



=



 indexPath



.



row 



%



2



 



?



 



[



UIColor



 orangeColor



]



 



:



 



[



UIColor



 magentaColor



];




 







    



//字体家族名称




    



NSString



 



*



familyName



=



 



[[



UIFont



 familyNames



]



 objectAtIndex



:



indexPath



.



section



];







    



//字体家族中的字体库名称




    



NSString



 



*



fontName  



=



 



[[



UIFont



 fontNamesForFamilyName



:[[



UIFont



 familyNames



]



 objectAtIndex



:



indexPath



.



section



]]



 objectAtIndex



:



indexPath



.



row



];



    




 







    cell



.



textLabel



.



font 



=



 



[



UIFont



 fontWithName



:



fontName size



:



14.0f



];




    




    



//查找微软雅黑字体




    



if



([



fontName isEqualToString



:@



"MicrosoftYaHei"



])



 



{




        



NSLog



(@



"微软雅黑"



);




    



}



    




    cell



.



textLabel



.



text 



=



 



[



NSString



 stringWithFormat



:@



"%@ - %@"



,



 familyName



,



 fontName 



];




    




    



return



 cell



;






}






 

在iOS应用中加载自定义字体显示 - 千相思 - 专注于移动应用开发

 这样可以获得系统所支持的所有字体类型。

但问题是,设计师在设计UI效果图时经常会使用其他的字体,怎么样才能使我们的应用支持这些字体显示了?

解决方法其实也很简单,  你自需如下几步就可以实现自定义的字体显示了。(在此以常用的 微软雅黑 字体 为例)

1.   找到你需要的字体库.ttf文件,导入到项目工程中

在iOS应用中加载自定义字体显示 - 千相思 - 专注于移动应用开发

 

2.   在Info.plist文件中,加入自定义字体库支持的说明

在iOS应用中加载自定义字体显示 - 千相思 - 专注于移动应用开发

 
3.   在系统提供的UIFont类中,查找到你需要的字体库再设置到需要显示的控件上即可。

在iOS应用中加载自定义字体显示 - 千相思 - 专注于移动应用开发

 
现在在列表中已经能看到我们自定义的字体库 微软雅黑 (MicrosoftYaHei) 。  在列表中显示的效果图如下所示:

在iOS应用中加载自定义字体显示 - 千相思 - 专注于移动应用开发

 
就这么简单!

 

 

From: http://stcui.me/131-ios-%E8%87%AA%E5%AE%9A%E4%B9%89%E5%AD%97%E4%BD%93/

 

1 将字体添加到工程中
2 在info.plist中的UIAppFonts(Fonts provided by application)中添加字体文件名如wqy-zenhei.ttc
2 引用字体 [UIFont fontWithName:@"字体名(非文件名)如WenQuanYi Zen Hei" size:fontSize]

支持的字体形式:
TrueType-flavoured OpenType (extension .ttf)和
PostScript-flavoured OpenType (extension .otf)。

 

 

From: http://www.189works.com/article-96736-1.html

准备 :你的自定义字体文件-〉ttf,odf字体文件。

现在网上一般下的中文字体文件都是ttc格式的,这个需要转换一下,网上有很多转换工具,不过都是windows下面的。(ttc就是多个ttf压在一起形成的)

使用 :

1.加入工程中

2.得到UIFont来使用

方法一:

Info.plist中添加Fonts provided by application项,加入一个item值为刚刚添加的字体文件文件名。

如果不知道这个字体的FontName,可以使用Mac OS中的 字体册 程序查看。

[textField setFont:[UIFont fontWithName:@"



Amelia BT



"



 size:12



]];

方法二:

直接使用代码来获取UIFont

-(UIFont*)customFont{// 你的字体路径    NSString 



*fontPath = [[NSBundle mainBundle] pathForResource:@"



Amelia BT



"



 ofType:@"



ttf



"



];        NSURL 



*url = [NSURL fileURLWithPath:fontPath];        CGDataProviderRef fontDataProvider 



= CGDataProviderCreateWithURL((__bridge CFURLRef)url);    



if



 (fontDataProvider == NULL)        return



 nil;    CGFontRef newFont 



= CGFontCreateWithDataProvider(fontDataProvider);    CGDataProviderRelease(fontDataProvider);    



if



 (newFont == NULL) return



 nil;        NSString 



*fontName = (__bridge NSString *)CGFontCopyFullName(newFont);    UIFont 



*font = [UIFont fontWithName:fontName size:12



];        CGFontRelease(newFont);        



return



 font;}



 

修正:

1.方法二中如果要使用UIFont,那么 不添加 Fonts provided by application项是不可以的。如果仅仅使用

CGFontRef,那么可以不添加。
2.测试中没有一次用ttc提取的ttf成功改变字体(中文的),但是下载的源文件就是ttf的都成功更改了字体。



 

 

http://www.cnblogs.com/qiqibo/archive/2012/09/27/2706060.html

在程序中加载ttf文件

首先要解决的问题,是在程序中加载.ttf/otf文件。我们在示例程序的资源束中加入了一个“方正大黑简体.ttf”的文件。这个文件是我从Mac系统中搜索到的,应该是MicrosoftOffice中提供的字体文件。我们用以下代码来加载它:

NSString *fontPath = [[ NSBundle mainBundle ] pathForResource : @" 方正大黑简体 " ofType: @"ttf" ];

CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename ([fontPath UTF8String ]);

font_ref = CGFontCreateWithDataProvider (fontDataProvider);

currentTable = readFontTableFromCGFont ( font_ref );

CGDataProviderRelease (fontDataProvider);

提示:在iOS3.2以后,还可以在plist文件中添加 UIAppFonts键的方式添加自定字体。见苹果文档“Custom Font Support ”主题。

这个简单的写法

 

From: http://blog.sina.com.cn/s/blog_8732f193010129zt.html

iOS 中用代码写字体,并加入粗体斜体等效果

本来以为那些效果要单独处理的,其实很简单,直接放到字体的名字里。

 

比如我想要个粗体加斜体,如下即可:

_lbRedNum.font = [UIFont fontWithName:@"Helvetica-BoldOblique" size:25.0f];

 

具体的这些“BoldOblique”的写法可以随便弄个Xib看一下嘛,嘿嘿

 

 

From: http://tieba.baidu.com/p/1942702351

 

之前的好几个项目,客户都要求使用微软雅黑字体,可是iOS没有自带这个字体,肿么办
只能自己自定义字体了,下面是自定义字体的几个重要步骤:
1、下载字体资源文件(.ttf或.otf格式的文件)
比如说你要使用微软雅黑字体,就需要下载微软雅黑字体文件。
需要注意的是,.otf格式的文件只能在iOS之后才能使用,所以假如你的系统需要在iOS5.0之前的版本中使用的话,最好使用.ttf格式的字体文件
2、在info.plist中添加字体配置
将下载的字体文件添加到工程中,同时在info.plist文件中,添加“Fonts provided by application”配置项,比如说我的微软雅黑字体资源文件名为:msyh.ttf,则在info.plist中的添加该项之后的效果如下:
3、在代码中使用自定义字体
使用示例如下:
[cpp]
UITextView *msg = [[UITextView alloc] init];
msg.font = [UIFont fontWithName:@"MicrosoftYaHei" size:18.0f];//MicrosoftYaHei为字体的名称,此处为微软雅黑字体

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。