CoreText 浅析
最近项目有这样一个需求,UITableViewCell中有一段文本,最多显示6行,超过6行就折叠为4行,还可点击全文
展开全文。为了避免UITaleView滑动的时候cell高度不确定带来的页面卡顿问题,就寻思能否提前获取文本的高度,因为有文本折叠的问题,需要知道文本一共有多少行,折叠为4行高度是多少,展开全文高度又是多少,顺着这个思路就发现了CoreText,然后再结合YYText中YYLabel的源码,总结一下
先附一张实现代码:
//string转AttributedString
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:self.text];
//
CFMutableAttributedStringRef attrString = (__bridge CFMutableAttributedStringRef)attStr;
//设置字体
CTFontRef font = CTFontCreateUIFontForLanguage(kCTFontUserFontType, [Font15 pointSize], NULL);
CFAttributedStringSetAttribute(attrString, CFRangeMake(0, CFAttributedStringGetLength(attrString)), kCTFontAttributeName, font);
//创建 CTFramesetterRef
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
// 文本绘制区域,宽度必须明确,高度给了一个超大值,不然会出现绘制不全的情况
CGPathRef path = CGPathCreateWithRect(CGRectMake(0,0,kbScreenWidth-30,10000), NULL);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
// 获取所有行数组
CFArrayRef lines = CTFrameGetLines(frame);
// 有多少行
size_t numOfLines = CFArrayGetCount(lines);
// 计算出每一行的origins坐标,存放在一个数组
CGPoint lineOrigins[numOfLines];
CTFrameGetLineOrigins(frame, CFRangeMake(0, 0), lineOrigins);
//总的高度
CTLineRef line = CFArrayGetValueAtIndex(lines, numOfLines-1);
self.fullHeight = lineOrigins[0].y-lineOrigins[numOfLines-1].y+CTLineGetBoundsWithOptions(line,kCTLineBoundsExcludeTypographicLeading).size.height;
//如果超过指定行数,就计算部分高度
if (numOfLines > self.foldLineThreshold) {
CTLineRef line = CFArrayGetValueAtIndex(lines, self.foldLineCount-1);
self.foldHeight = lineOrigins[0].y-lineOrigins[self.foldLineCount-1].y+CTLineGetBoundsWithOptions(line,kCTLineBoundsExcludeTypographicLeading).size.height;
self.foldFlag = 1;
} else {
self.foldFlag = 0;
}
//用完了记得释放哦
CFRelease(framesetter);
CFRelease(frame);
CoreText实现
image看上图,我们可以知道,一个View包括CTFrame
,CTFrame
中间包括许多行CTLine
,而一个CTLine
中包括许多CTRun
我们主要说说CTLine
和CTRun
CTLine
其实比较好理解,当我们用UILabel
显示文本内容时,一行就是一个CTLine
,一个CTLine
中有一个或者多个CTRun
,CTRun
的个数取决于文本的具体内容,如果一行里面有两种大小的文字或者两种颜色不通的文字,可以简单的认为这一行就有两个CTRun
,其中CTRun
还可以是图片
我们再来说说文字结构
imageOrigin是原点,后面获取的坐标都是该坐标,宽度是可以直接获取的,而高度则是由Ascent和Descent相加得到
具体实现
具体实现过程中主要用到了NSMutableAttributedString
中的Attributes
NSMutableAttributedString *astring = _textString;
//获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//设置坐标系
//设置字形的变换矩阵为不做图形变换
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
//平移方法,将画布向上平移一个屏幕高度
CGContextTranslateCTM(context, 0, self.bounds.size.height);
//缩放方法,x轴缩放系数为1,则不变,y轴缩放系数为-1,则相当于以x轴为轴旋转180度
CGContextScaleCTM(context, 1, -1);
//创建path
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, self.bounds);
CTFramesetterRef frameRef = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)astring);
CTFrameRef frame = CTFramesetterCreateFrame(frameRef, CFRangeMake(0, astring.length), path, NULL);
CTFrameDraw(frame, context);
CFRelease(path);
CFRelease(frame);
CFRelease(frameRef);
首先是获取上下文
由于屏幕的坐标系是原点在左上角,然后x,y轴的箭头方向分别是向右和向下,与Quartz 2D
坐标系不一样,Quartz 2D
坐标系是原点在左下角,x,y轴的方向分别是向右和向上,CGContextSetTextMatrix () ,CGContextTranslateCTM (), CGContextScaleCTM ()
这三句主要就是转换坐标系,感兴趣的同学可以试着改改其中的参数
然后再创建path,这个path主要是为了后面的CTFrame
服务
根据文本内容创建CTFramesetterRef
根据CTFramesetterRef
Range
path
来得到CTFrameRef
最后通过CTFrameDraw
来将CTFrameRef
绘制在上下文中
因为这些都是C方法,所以要自己来释放,通过CFRelease
来释放
加这段代码放到View的drawRect
中就可以运行了.