iOS开发用自定义ttf字体

Phone系统的字体数量有限,并且多数对中文没有效果,下面介绍两种解决办法

方法1

  添加对应的字体(.ttf.odf)到工程的resurce,使用cocos2d中的FontLabel库,FontLabel继承于UILabel,象UILabel一样使用就好了

  fontName直接使用添加的资源名字即可.

     这方法我没有研究过,下面这个方法是可以的,并且有demo。

方法2:

  1,添加对应的字体(.ttf.odf)到工程的resurce,例如simkai.ttf

  2,在info.plist中添加一项 Fonts provided by application (item0对应的valuesimkai.ttf,添加多个字体依次添加就可以了)

 

  3,使用时 aLabel.font=[UIFont fontWithName:@"XXX" size:30]; 注意XXX不一定是DFPShaoNvW5-GB1111(DFPShaoNvW5-GB1111文件名不代表字体名称,这里是DFPShaoNvW5-GB,你可以通过下面的方法遍历所有字体

[plain]  view plain copy
  1. - (void)showAllFonts  
  2. {  
  3.     //显示系统中所有的字体  
  4.     NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];  
  5.     for (int indFamily=0; indFamily<[familyNames count]; ++indFamily) {  
  6.         NSMutableString *string = [NSMutableString stringWithFormat:@"Family name:%@ (Font name:", [familyNames objectAtIndex:indFamily]];  
  7.         NSArray *fontNames = [[NSArray alloc] initWithArray: [UIFont fontNamesForFamilyName:[familyNames objectAtIndex:indFamily]]];  
  8.         for (int indFont=0; indFont<[fontNames count]; ++indFont) {  
  9.             [string appendFormat:@"%@, ", [fontNames objectAtIndex:indFont]];  
  10.         }  
  11.         [string appendFormat:@")"];  
  12.         CFShow(string);  
  13.         [fontNames release];  
  14.     }  
  15.     [familyNames release];  
  16. }  

使用方法:

[plain]  view plain copy
  1. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 30, 200, 60)];  
  2. label.font = [UIFont fontWithName:@"DFPShaoNvW5-GB" size:20];  
  3. label.backgroundColor = [UIColor clearColor];  
  4. label.text = @"中文abc123";  
  5. [self.view addSubview:label];  
  6. [label release];  

效果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值