Skip to content

Instantly share code, notes, and snippets.

@lqik2004
Created July 22, 2012 10:05
Show Gist options
  • Save lqik2004/3159157 to your computer and use it in GitHub Desktop.
Save lqik2004/3159157 to your computer and use it in GitHub Desktop.
Custom CoreText Fonts
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])];
@lqik2004
Copy link
Author

lqik2004 commented Aug 4, 2012

不过有个奇怪的问题, CFRelease(theCGFont);不起作用,而且貌似这个问题只发生在中文字体上(非英文字体更准确一点?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment