ios-coreText做微信点赞功能
2016-08-09 14:35
441 查看
coretext绘制 个人理解为
一个CTFrame有几个CTLine组成,有几行文字就有几行CTLine。一个CTLine有包含多个CTRun,一个CTRun是所有属性都相同的那部分富文本的绘制单元。所以CTRun是CTFrame的
资料博客链接地址:http://www.jianshu.com/p/6db3289fb05d
计算绘制的coreText内容的高度
+ (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 = (__bridge 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;
}
一个CTFrame有几个CTLine组成,有几行文字就有几行CTLine。一个CTLine有包含多个CTRun,一个CTRun是所有属性都相同的那部分富文本的绘制单元。所以CTRun是CTFrame的
基本绘制单元。
资料博客链接地址:http://www.jianshu.com/p/6db3289fb05d
计算绘制的coreText内容的高度
+ (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 = (__bridge 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;
}
相关文章推荐
- IOS利用Core Text对文字进行排版
- iOS开源:CoreTextWrapper
- ios开发之使用CorePlot绘制折线图并添加拖动功能
- IOS CoreText.framework --3--- 行 CTLineRef
- IOS CoreText.framework ----1--- 基本用法
- IOS开源:CoreTextWrapper
- iOS 学习笔记10 core Text 对齐方式
- 【iOS开发】 CoreText 使用教程:以创建一个简单的杂志应用为例
- IOS利用Core Text对文字进行排版
- Core Text Tutorial for iOS: Making a Magazine App
- IOS利用Core Text对文字进行排版
- iOS~CoreText入门
- ios之Core Location定位功能
- IOS利用Core Text对文字进行排版
- IOS CoreText.framework --- 图文混排
- Pinger为iOS版textfree增加语音邮件功能
- IOS利用Core Text对文字进行排版
- IOS CoreText.framework --- 段落样子CTParagraphStyle
- iOS开发】 CoreText 使用教程:以创建一个简单的杂志应用为例
- Core Text Tutorial for iOS: Making a Magazine App(中文)