Core Animation - CATextLayer和富文本

本文介绍如何使用CATextLayer替代UILabel进行高效文本渲染,包括设置文本属性、字体及支持Retina显示屏的方法。此外,还讲解了如何利用CATextLayer实现富文本效果。

1.CATextLayer
为什么要存在CATextLayer呢?它的意义是什么?我们可以用UILabel来写文字,但两者不是一码事,CATextLayer的渲染速度比UILabel快的多,而UILabel的精髓就是用图层代理把字符串用Core Graphics写入如层,CATextLayer则不用,它以涂层的形式几乎包含了UILabel所有的特性,并有额外的新特性。

 //create a text layer
    CATextLayer *textLayer = [CATextLayer layer];
    textLayer.frame = self.labelView.bounds;
    [self.labelView.layer addSublayer:textLayer];
    //set text attributes
    textLayer.foregroundColor = [UIColor blackColor].CGColor;
    textLayer.alignmentMode = kCAAlignmentJustified;
    textLayer.wrapped = YES;
    //choose a font
    UIFont *font = [UIFont systemFontOfSize:15];
    //set layer font
    CFStringRef fontName = (__bridge CFStringRef)font.fontName;
    CGFontRef fontRef = CGFontCreateWithFontName(fontName);
    textLayer.font = fontRef;
    textLayer.fontSize = font.pointSize;
    CGFontRelease(fontRef);
//choose some text
NSString *text = @"Lorem ipsum dolor sit amet, consectetur adipiscing \ elit.Lorem ipsum dolor sit amet, consectetur adipiscing \ elit.Lorem ipsum dolor sit amet, consectetur adipiscing \ elit.Lorem ipsum dolor sit amet, consectetur adipiscing \ elit.Lorem ipsum dolor sit amet, consectetur adipiscing \ elit.";
 textLayer.string = text;
 //单写上面的会发现文字显示像素不高,那是因为没有支持retain显示屏,前面说过,要支持retain,需要设置contentsScale
 textLayer.contentsScale = [UIScreen mainScreen].scale;

2.富文本
这是一个在编程中经常用到的东西,使用富文本需要添加库 #import

//create a text layer
    CATextLayer *textLayer = [CATextLayer layer];
    textLayer.frame = self.labelView.bounds;
    textLayer.contentsScale = [UIScreen mainScreen].scale;
    [self.labelView.layer addSublayer:textLayer];
    //set text attributes
    textLayer.alignmentMode = kCAAlignmentJustified;
    textLayer.wrapped = YES;
    //choose a font
 UIFont *font = [UIFont systemFontOfSize:15];
//choose some text
NSString *text = @"Lorem ipsum dolor sit amet, consectetur adipiscing \ elit. Quisque massa arcu, eLorem ipsum dolor sit amet, consectetur adipiscing \ elit. Quisque massa arcu, eLorem ipsum dolor sit amet, consectetur adipiscing \ elit. Quisque massa arcu, eLorem ipsum dolor sit amet, consectetur adipiscing \ elit. Quisque massa arcu, e";
    //create attributed string
    NSMutableAttributedString *string = nil;
    string = [[NSMutableAttributedString alloc] initWithString:text];
    //convert UIFont to a CTFont
    CFStringRef fontName = (__bridge CFStringRef)font.fontName;
    CGFloat fontSize = font.pointSize;
    CTFontRef fontRef = CTFontCreateWithName(fontName, fontSize, NULL);
    //set text attributes
    NSDictionary *attribs = @{
(__bridge id)kCTForegroundColorAttributeName:(__bridge id)[UIColor blackColor].CGColor,
      (__bridge id)kCTFontAttributeName: (__bridge id)fontRef
    };
[string setAttributes:attribs range:NSMakeRange(0, [text length])]; attribs = @{
(__bridge id)kCTForegroundColorAttributeName: (__bridge id)[UIColor redColor].CGColor, (__bridge id)kCTUnderlineStyleAttributeName: @(kCTUnderlineStyleSingle),
(__bridge id)kCTFontAttributeName: (__bridge id)fontRef
    };
    [string setAttributes:attribs range:NSMakeRange(6, 5)];
    //release the CTFont we created earlier
    CFRelease(fontRef);
    //set layer text
    textLayer.string = string;
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodingFire

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值