iphone游戏开发中使用自定义字体的方法

http://www.cocoachina.com/bbs/read.php?tid-27791.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],这样就可以了。



20140612添加


    CTFontRef font = CTFontCreateWithName((CFStringRef)self.fontName, self.fontSize, NULL);

    CGFontRef fontNoBold = CTFontCreateCopyWithSymbolicTraits(font, self.fontSize, nil, ~kCTFontBoldTrait, ~kCTFontBoldTrait);

NSDictionary *attributes = [[NSDictionary alloc] initWithObjectsAndKeys:

                                ( id)fontNoBold, (id)kCTFontAttributeName,

                                _textColor.CGColor,(id)kCTForegroundColorAttributeName,

                                nil];

    [aString addAttributes:attributes range:NSMakeRange(0,[aString length])];

    [attributes release];

    CFRelease(font);

#else 

    //

    NSDictionary *ligatureFeatureSetting = [NSDictionary dictionaryWithObjectsAndKeys:

                                            [NSNumber numberWithInt:kLigaturesType], kCTFontFeatureTypeIdentifierKey,

                                            [NSNumber numberWithInt:kRequiredLigaturesOffSelector], kCTFontFeatureSelectorIdentifierKey,

                                            nil];

//    NSDictionary *shapeFeatureSetting = [NSDictionary dictionaryWithObjectsAndKeys:

//                                            [NSNumber numberWithInt:kCharacterShapeType], kCTFontFeatureSelectorSettingKey,

//                                            nil];

    NSDictionary *fontAttributes =

    [NSDictionary dictionaryWithObjectsAndKeys:

     @"Courier", (NSString *)kCTFontFamilyNameAttribute,

     @"", (NSString *)kCTFontStyleNameAttribute,

     [NSNumber numberWithFloat:16.f], (NSString *)kCTFontSizeAttribute,

     nil];

    

    

    NSDictionary *numberFeatureSetting = [NSDictionary dictionaryWithObjectsAndKeys:

                                          [NSNumber numberWithInt:kNumberCaseType], kCTFontFeatureTypeIdentifierKey,

                                          [NSNumber numberWithInt:kUpperCaseNumbersSelector], kCTFontFeatureSelectorIdentifierKey,

                                          nil];    

    NSArray *featuresArray = [NSArray arrayWithObjects:ligatureFeatureSetting, numberFeatureSetting, fontAttributes, nil];


    CTFontDescriptorRef fontDescriptor = CTFontDescriptorCreateWithAttributes(

                                                                              (CFDictionaryRef) [NSDictionary dictionaryWithObjectsAndKeys:self.fontName, (NSString*)kCTFontNameAttribute,  featuresArray, (NSString*)kCTFontFeatureSettingsAttribute, nil] );

    CTFontDescriptorRef descriptor =

    CTFontDescriptorCreateWithAttributes((CFDictionaryRef)fontAttributes);

    CTFontRef font = CTFontCreateWithFontDescriptor(descriptor, 0, NULL);

//    CGFloat pointSize = CTFontGetSize(fontRef);

//    CTFontRef newFont = CTFontCreateWithFontDescriptor(fontDescriptor, self.fontSize, NULL);

//    CGFontRef fontNoBold = CreateBoldFont(newFont, 0);

    [aString addAttributes:[NSDictionary dictionaryWithObject:(id)font forKey:(NSString*)kCTFontAttributeName] range:NSMakeRange(0, aString.length)];

    CFRelease(font);

#endif



    CTFontRef font = CTFontCreateWithName((CFStringRef)self.fontName, self.fontSize, NULL);

    CGFontRef fontNoBold = CTFontCreateCopyWithSymbolicTraits(font, self.fontSize, nil, ~kCTFontBoldTrait, ~kCTFontBoldTrait);

NSDictionary *attributes = [[NSDictionary alloc] initWithObjectsAndKeys:

                                ( id)fontNoBold, (id)kCTFontAttributeName,

                                _textColor.CGColor,(id)kCTForegroundColorAttributeName,

                                nil];

    [aString addAttributes:attributes range:NSMakeRange(0,[aString length])];

    [attributes release];

    CFRelease(font);

#else 

    //

    NSDictionary *ligatureFeatureSetting = [NSDictionary dictionaryWithObjectsAndKeys:

                                            [NSNumber numberWithInt:kLigaturesType], kCTFontFeatureTypeIdentifierKey,

                                            [NSNumber numberWithInt:kRequiredLigaturesOffSelector], kCTFontFeatureSelectorIdentifierKey,

                                            nil];

//    NSDictionary *shapeFeatureSetting = [NSDictionary dictionaryWithObjectsAndKeys:

//                                            [NSNumber numberWithInt:kCharacterShapeType], kCTFontFeatureSelectorSettingKey,

//                                            nil];

    NSDictionary *fontAttributes =

    [NSDictionary dictionaryWithObjectsAndKeys:

     @"Courier", (NSString *)kCTFontFamilyNameAttribute,

     @"", (NSString *)kCTFontStyleNameAttribute,

     [NSNumber numberWithFloat:16.f], (NSString *)kCTFontSizeAttribute,

     nil];

    

    

    NSDictionary *numberFeatureSetting = [NSDictionary dictionaryWithObjectsAndKeys:

                                          [NSNumber numberWithInt:kNumberCaseType], kCTFontFeatureTypeIdentifierKey,

                                          [NSNumber numberWithInt:kUpperCaseNumbersSelector], kCTFontFeatureSelectorIdentifierKey,

                                          nil];    

    NSArray *featuresArray = [NSArray arrayWithObjects:ligatureFeatureSetting, numberFeatureSetting, fontAttributes, nil];


    CTFontDescriptorRef fontDescriptor = CTFontDescriptorCreateWithAttributes(

                                                                              (CFDictionaryRef) [NSDictionary dictionaryWithObjectsAndKeys:self.fontName, (NSString*)kCTFontNameAttribute,  featuresArray, (NSString*)kCTFontFeatureSettingsAttribute, nil] );

    CTFontDescriptorRef descriptor =

    CTFontDescriptorCreateWithAttributes((CFDictionaryRef)fontAttributes);

    CTFontRef font = CTFontCreateWithFontDescriptor(descriptor, 0, NULL);

//    CGFloat pointSize = CTFontGetSize(fontRef);

//    CTFontRef newFont = CTFontCreateWithFontDescriptor(fontDescriptor, self.fontSize, NULL);

//    CGFontRef fontNoBold = CreateBoldFont(newFont, 0);

    [aString addAttributes:[NSDictionary dictionaryWithObject:(id)font forKey:(NSString*)kCTFontAttributeName] range:NSMakeRange(0, aString.length)];

    CFRelease(font);

#endif


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值