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