2023年7月14日发(作者:)
YYText源码解析——YYLabel(⼀)架构(引⽤作者框架对⽐图⽚)YYText包括控件(YYLabel,YYTextView),布局(YYTextLayout,NSAttributedString,YYTextContainer),核⼼(CoreText)。源码⽬录组织YYLabel继承关系YYLabel 直接继承 UIView,作者⾃⼰实现 Label 的渲染。+_+statestruct {
// 是否需要更新布局unsigned int layoutNeedUpdate : 1;unsigned int showingHighlight : 1;unsigned int trackingTouch : 1;unsigned int swallowTouch : 1;unsigned int touchMoved : 1;unsigned int hasTapAction : 1;unsigned int hasLongPressAction : 1;// 内容是否⽀持逐渐消失unsigned int contentsNeedFade : 1; } _state;如何熏染出 TextYYLabel 的熏染是通过
YYTextAsyncLayerDelegate 完成。
YYTextAsyncLayerDelegate 提供了
newAsyncDisplayTask 配置⽅法⽤来实现渲染。YYTextAsyncLayer 与 YYLabel 的熏染流程已
setText 为例,看看这个熏染的流程。[ setNeedsDisplay] 这⾥调⽤的是
YYTextAsyncLayer 的
setNeedsDisplay 。因为
layerClass ⽅法被重写了,返回了
YYTextAsyncLayer。所以主layer所使⽤的类是
YYTextAsyncLayer 。_cancelAsyncDisplay 的实现,后续再探讨。。。记遗留问题。这⾥
display 也被重写了,在这了执⾏了渲染最为关键的函数
_displayAsync 。YYTextAsyncLayer 提供了
displaysAsynchronously ⽤来控制是否进⾏异步渲染,默认为异步熏染。熏染本⼈对
Core Graphics Framework 部分不同熟悉,个⼈重点对这⼀块代码进⾏学。刚才我们发现这⾥的渲染分为:普通渲染(ps:不知道怎么称,叫同步渲染感觉不太适合,就叫普通渲染吧)和异步渲染。这两种渲染的渲染代码处理基本是⼀样的,只是异步熏染增加了⼀些多线程的处理的考虑(⽐如:多线程经典的“读与写”的问题)。所以就直接学习普通渲染部分的代码吧#^_^#YYTextAsyncLayer 熏染学习UIGraphicsBeginImageContextWithOptions(, , tsScale);CGContextRef context = UIGraphicsGetCurrentContext();if () { CGSize size = ; *= tsScale; *= tsScale; CGContextSaveGState(context); { if (!oundColor || CGColorGetAlpha(oundColor) < 1) { CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor); CGContextAddRect(context, CGRectMake(0, 0, , )); CGContextFillPath(context); } if (oundColor) { CGContextSetFillColorWithColor(context, oundColor); CGContextAddRect(context, CGRectMake(0, 0, , )); CGContextFillPath(context); } } CGContextRestoreGState(context);}y(context, , ^{return NO;});UIImage *image = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();ts = (__bridge id)(e);开始是调⽤
UIGraphicsBeginImageContextWithOptions 创建⼀个图⽚处理的上下⽂。 绘制结束调⽤
UIGraphicsEndImageContext 。通过
UIGraphicsGetCurrentContext 获取当前图形的上下⽂。这⾥有个
opaque 属性表⽰是否不透明,详细属性参见。CGContextSaveGState 把上下⽂保存到栈中。然后通过
CGContextSetFillColorWithColor 设置填充颜⾊,
CGContextAddRect 添加矩形,CGContextFillPath 填充上下⽂路径。 最后保存通过
CGContextRestoreGState 保存最近的上下⽂。调⽤
y 回调,这⾥是调⽤配置的渲染⽅法,后⾯再分析
YYLabel 的熏染回调。
YYLabel 的熏染主要通过
YYTextLayout 来处理。这么后续再做学习。通过
UIGraphicsGetImageFromCurrentImageContext 当前的上下⽂绘制出来的图⽚,结束绘制。最后将图⽚赋值给
contents 属性完成熏染。
发布者:admin,转转请注明出处:http://www.yc00.com/news/1689264064a226403.html
评论列表(0条)