iOS开发 准确计算Coretext高度
2014-03-16 09:24
507 查看
- (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;
}
//关于line坐标位置y为下图黑线所在位置 descent为黑线下部分字体的高度
//关于字体各部分高度说明 http://ios-iphone.diandian.com/post/2012-03-29/18055023
{
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;
}
//关于line坐标位置y为下图黑线所在位置 descent为黑线下部分字体的高度
//关于字体各部分高度说明 http://ios-iphone.diandian.com/post/2012-03-29/18055023
相关文章推荐
- iOS开发 CoreText中的高度计算问题
- 30、准确计算CoreText高度的方法
- 准确计算CoreText高度的方法:
- 准确计算CoreText高度的方法
- IOS core text计算文本高度及最大宽度
- iOS开发学习之 - 高度自适应之计算文字高度
- 【iOS开发】 CoreText 使用教程:以创建一个简单的杂志应用为例
- 【iOS 开发】Xcode6中计算文字的宽度或高度 ——伊禾媛
- iOS开发小记:iOS富文本框架DTCoreText在UITableView上的使用
- iOS开发动态计算cell的高度
- iOS开发小技巧--根据文字,计算label中文字高度
- ios开发问题,经验证,textview的contentsize.height不能准确判断高度
- iOS开发之计算动态cell的高度并缓存
- iOS开发】 CoreText 使用教程:以创建一个简单的杂志应用为例
- iOS开发小技巧--iOS8之后的cell自动计算高度
- 【iOS开发】 CoreText 使用教程:以创建一个简单的杂志应用为例
- iOS开发 Coretext基本用法
- iOS开发 Coretext基本用法
- IOS开发中UILabel单行、多行文本计算高度、宽度的技巧