Created
July 22, 2012 10:05
-
-
Save lqik2004/3159157 to your computer and use it in GitHub Desktop.
Custom CoreText Fonts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSString *fontPath = [[NSBundle mainBundle] pathForResource:_fontName ofType:@"otf"]; | |
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fontPath UTF8String]); | |
// 从DataProvider创建字体 | |
CGFontRef theCGFont =CGFontCreateWithDataProvider(fontDataProvider); | |
CTFontRef theCTFont=CTFontCreateWithGraphicsFont(theCGFont,_fontSize,NULL,NULL); | |
CFRelease(theCGFont); | |
CFRelease(fontDataProvider); | |
//创建AttributeString | |
attrString=[[NSMutableAttributedString alloc]initWithString:self.text]; | |
//设置字体及大小 | |
//CTFontRef helveticaBold = CTFontCreateWithName((__bridge CFStringRef)_fontName,_fontSize,NULL); | |
[attrString addAttribute:(id)kCTFontAttributeName value:(__bridge id)theCTFont range:NSMakeRange(0,[attrString length])]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
不过有个奇怪的问题, CFRelease(theCGFont);不起作用,而且貌似这个问题只发生在中文字体上(非英文字体更准确一点?)