您的位置:首页 > 其它

CoreText的使用方法

2015-09-29 22:30 316 查看
- (void)draw {
CGContextRef context = UIGraphicsGetCurrentContext();

NSMutableAttributedString *attrString = [[[NSMutableAttributedString alloc] initWithString:self.text] autorelease];
[attrString addAttribute:(NSString *)(kCTForegroundColorAttributeName) value:(id)self.strokeColor.CGColor range:NSMakeRange(0, [self.text length])];

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrString);

CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, [attrString length]), self.path.CGPath, NULL);
CFRelease(framesetter);
//CFRelease(attrString);
if (frame) {
CGContextSaveGState(context);

// Core Text wants to draw our text upside-down!  This flips it the
// right way.
CGContextTranslateCTM(context, 0, path.bounds.origin.y);
CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, 0, -(path.bounds.origin.y + path.bounds.size.height));

CTFrameDraw(frame, context);

CGContextRestoreGState(context);

CFRelease(frame);
}
}


首先获得当前的上下文

创建一个属性自字符串NSMutableAttributedString 并设置他的颜色以及其他属性

利用该属性字符串 创建一个CTFramesetterRef

再创建一个CTFrameRef

释放之前创建的CTFramesetterRef 对象framesetter

由于CoreText 是来自于Mac OS X的 它在绘图的时候 认为坐标轴是倒置的,所以在没ios中会产生倒置的效果,这里要转化以下才能正常显示

参考:http://www.cnblogs.com/bucengyongyou/archive/2012/09/14/2685797.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: