您的位置:首页 > 移动开发 > IOS开发

IOS(xcode)程序中使用自定义字体的方法

2014-04-15 10:42 671 查看
1.将需要的字体库xxx.ttf添加到工程中,注意一定要在copy bundle resources中存在,如果没有添加上去







2.在info.plist 文件中添加 fonts provided by application 默认为array,也可以使用dictionary  添加value为  xxx.ttf 

可以添加多个,使用的时候写对应字体名字就行。

如下图:





3.在你的工程就可以直接用了。xx.font = [UIFont fontWithName:@"FZZhunYuan-M02S" size:20.0];(注意:这里的字体名字为familyName,不是文件名)通过遍历字体,可以得到所添加字体的familyName,应该是在数组的最后面。

[plain] view
plaincopyprint?

//    遍历所有字体。这是已经把新字体添加进去了  

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

        }  

    }  

我就是不知道是familyName才试了好几遍,╮(╯▽╰)╭

更多字体:http://font.knowsky.com/down/6513.html

1、确定你的项目工程的resouce下有你要用的字体文件(.ttf,.odf)。 

2、然后在你的工程的Info.plist文件中新建一行(Add Row),添加key为:UIAppFonts,类型为Array或Dictionary都行;在UIAppFonts下再建立一个键值对,key为:Item 0,添加Value为XXX.ttf(你字体的名字,string型),可以添加多个,使用的时候写对应字体名字就行。

3、在你的项目里要用字体的时候 xx.font = [UIFont fontWithName:@"XXX" size:20.0],这样就可以了。

 

参考:http://www.soft6.com/v9/2011/jckf_0928/161184.html

 

方法1:

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

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

  方法2;

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

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

  3,使用时 aLabel.font=[UIFont fontWithName:@"XXX" size:30]; 注意XXX不一定是simkai,这里是KaiTi_GB2312(中文楷体),你可以通过下面的方法遍历所有字体

  以下是代码片段:

  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

要使用字体fontWithName:用“Family name”或者“Font name”都可以。

而不是字体的文件名,弄错了将无法看到效果。

在你的项目里要用字体的时候 xx.font = [UIFont fontWithName:@"Microsoft JhengHei" size:20.0],这样就可以了。

 

  其中添加的simkai.ttf对应的字体就是KaiTi_GB2312

  UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 50)];

  label.font = [UIFont fontWithName:@"KaiTi_GB2312" size:30];

  label.text = @"中文楷体";

  [self.view addSubview:label];

  [label release];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息