Core Animation - CATextLayer和富文本

简介: Core Animation - 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;


目录
相关文章
|
iOS开发
Core Animation - 图层几何学<一>
Core Animation - 图层几何学<一>
98 0
Core Animation - 图层几何学<一>
Core Animation - 图层行为
Core Animation - 图层行为
83 0
Core Animation - 图层行为
|
iOS开发 MacOS
Core Animation - 图层几何学<二>
Core Animation - 图层几何学<二>
89 0
Core Animation - 视觉效果<二>
Core Animation - 视觉效果<二>
53 0
|
iOS开发
Core Animation - 视觉效果<一>
Core Animation - 视觉效果<一>
93 0
|
算法 iOS开发
Core Animation - 视觉效果<三>
Core Animation - 视觉效果<三>
70 0
|
图形学
Core Animation -关键帧动画
Core Animation -关键帧动画
112 0
Core Animation - 渐变色CAGradientLayer
Core Animation - 渐变色CAGradientLayer
93 0
Core Animation - 图层时间
Core Animation - 图层时间
78 0
|
iOS开发
Core Animation - 第一次使用图层来创建一个简单的项目
Core Animation - 第一次使用图层来创建一个简单的项目
79 0