准确计算CoreText高度的方法:
2015-09-30 09:54
561 查看
- (int)getAttributedStringHeightWithString:(NSAttributedString *) string WidthValue:(int) width { int total_height = 0; CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string); //string 为要计算高度的NSAttributedString CGRect drawingRect = CGRectMake(0, 0, width, 1000); //这里的高要设置足够大 CGMutablePathRef path = CGPathCreateMutable(); CGPathAddRect(path, NULL, drawingRect); CTFrameRef textFrame = CTFramesetterCreateFrame(framesetter,CFRangeMake(0,0), path, NULL); CGPathRelease(path); CFRelease(framesetter); NSArray *linesArray = (NSArray *) CTFrameGetLines(textFrame); CGPoint origins[[linesArray count]]; CTFrameGetLineOrigins(textFrame, CFRangeMake(0, 0), origins); int line_y = (int) origins[[linesArray count] -1].y; //最后一行line的原点y坐标 CGFloat ascent; CGFloat descent; CGFloat leading; CTLineRef line = (CTLineRef) [linesArray objectAtIndex:[linesArray count]-1]; CTLineGetTypographicBounds(line, &ascent, &descent, &leading); total_height = 1000 - line_y + (int) descent +1; //+1为了纠正descent转换成int小数点后舍去的值 CFRelease(textFrame); return total_height; }
来自:http://blog.csdn.net/iunion/article/details/7925951
相关文章推荐
- mysql基础知识
- [LeetCode]Search in Rotated Sorted Array
- Android 动画效 4000 果 --Animation 动画(讲解了所有的Android动画效果,是一个值得收藏的帖子)
- 解决Sublime Text3 中文乱码问题
- hbase 性能调优
- plsql无法工具导入的问题
- Leetcode85: Convert Sorted Array to Binary Search Tree
- 如何动态设置django的model field的默认值
- 使用Apache Camel处理web service请求的三种方法
- IIS中给网站添加匿名访问用户图文教程
- 真正成功地配置TheOS越狱开发环境
- 传热学相关的无量纲数的物理意义
- Oracle之sql追踪
- 数据结构之父指针实现树
- javascript 的点点滴滴
- table中的check列遍历
- C#生成安装文件
- Android 即时音视频解决方案2——腾讯云
- java类和对象实例分析
- SQLite