CoreText实现图文混排

本文详细介绍如何使用CoreText框架实现跨平台的文字布局与渲染,包括文本属性设置、路径创建及帧的绘制过程,并提及了几个常用的开源框架。

//CoreText  跨平台,以lable为例

//引入CoreText.framework框架。

NSDictionary *attributes = @{

           NSFontAttributeName:[UIFont systemFontOfSize:14],

           NSForegroundColorAttributeName:[UIColor blackColor],

};

NSMutableAttributedString *attrtext = [[NSMutableAttributedStringalloc] initWithString:text attributes:attributes];

//1.设置文本属性

[attrtextaddAttribute:NSUnderlineStyleAttributeName value:@2 range:NSMakeRange(0, 3)];

//Foundation  --->CoreFoundation

NSString *s =@"tt";

CFStringRef s2 = (__bridgeCFStringRef)s;

//1.通过NSMutableAttributedString 文本属性对象  构造--->CTFramesetterRef

CTFramesetterRefframesetter =CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrtext);

//2.构造CGPath

CGMutablePathRef path =CGPathCreateMutable();

//    CGRectInset(<#CGRectrect#>, <#CGFloat dx#>, <#CGFloat dy#>)

CGPathAddRect(path, NULL,self.bounds);

//3.通过CTFramesetterRef和CGPath 构造出一个CTFrameRef

CTFrameRef frame =CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);

//4.取得绘制的上下文

CGContextRef ctx =UIGraphicsGetCurrentContext();

//坐标系统向下平移当前视图的高度

CGContextTranslateCTM(ctx,0, CGRectGetHeight(self.bounds));

//缩放倍数为-1,使用坐标指向反方向

CGContextScaleCTM(ctx, 1,-1.0);

//5.绘制frame

CTFrameDraw(frame, ctx);

//取得frmae中所有的行

CFArrayRef lines =CTFrameGetLines(frame);

//绘制一行

//   CTLineDraw(<#CTLineRef line#>, <#CGContextRef context#>) 

CTLineRef line;

CFArrayRef runs =CTLineGetGlyphRuns(line);

//    CTRunDraw(<#CTRunRefrun#>, <#CGContextRef context#>, <#CFRange range#>)

//常用开源框架

FTCoreText

DTCoreText

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值