IOS 自定义字体

  


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],这样就可以了。




  NSArray *familyName = [[NSArrayalloc]initWithArray:[UIFontfamilyNames]];

    NSLog(@"f---------%@",familyName);

    NSArray *fontNames;

    NSInteger indFamily,indFont;

    for (indFamily=0; indFamily < [familyNamecount]; ++indFamily) {

        NSLog(@"Family name--------%@",[familyNameobjectAtIndex:indFamily]);

        fontNames = [[NSArray alloc]initWithArray:[UIFontfontNamesForFamilyName:[familyName objectAtIndex:indFamily]]];

        for (indFont =0; indFont < [fontNamescount]; ++indFont) {

            NSLog(@"font name----%@",[fontNamesobjectAtIndex:indFont]);

        }

        [fontNames release];

    }

    [familyName release];

    

    

    //UniDreamLED----------UniDream_LED

    UILabel *labelFont = [[UILabelalloc]initWithFrame:CGRectMake(30,100, 160, 50)];

    labelFont.backgroundColor = [UIColorclearColor];

    labelFont.textColor = [UIColorblackColor];

    labelFont.text=@"123456";

    labelFont.font = [UIFontfontWithName:@"UniDreamLED" size:26.0];

    [self.view addSubview:labelFont];

    [labelFont release];

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值