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
相关文章推荐
- #define
- JSONObject 好坑啊
- 批梯度下降算法 matlab
- 【笔试面试】大数相乘
- 发射功率的修改
- 省市二级联动
- AOJ 0033 Ball (水题)
- 多线程的基本理解
- ADPCM算法浅析
- 测试用例的特点与原则
- 万达招商的关键策略
- 使用Fiddler捕获Java程序中的HTTP请求
- 数据存取
- C++ Primer 学习笔记与思考_11 try..catch语句及相关疑点
- 面试注意事项与技巧
- PowerDesigner 设计概念模型时属性不能重名的解决方案
- hdu 5469 Antonidas (dfs+剪枝)2015 ACM/ICPC Asia Regional Shanghai Online
- hdoj4617Weapon【空间直线间的距离】
- 再谈ZigBee中的绑定机制
- 安卓自定义带删除图标的输入框EditView