CoreAnimation----CATextLayer

CATextLayer


CoreAnimation----CATextLayer_第1张图片
大美女

1. CATextLayer

  • UILabel的精髓

在一个图层里面显示文字,可以借助图层代理直接将字符串用Core Graphics写到图层的内容里

  • CALayer的一个子类

以图层形式包含UILabel几乎所有的绘制的特性,并且提供了额外的一些新的特性 。

  • CATextLayer的优点

相较于UILabel渲染的速度更快
iOS6以及之前版本UILabel是通过WebKit进行渲染(文字一多,性能压力极大)
CATextLayer通过Core Text,渲染极快


CoreText是Mac OS和iOS系统中处理文本的low-level API, 不管是使用OC还是swift, 实际我们使用CoreText都还是间接或直接使用C语言在写代码。CoreText是iOS和Mac OS中文本处理的根基, TextKit和WebKit都是构建于其上


  • CATextLayer使用的注意点
  1. CATextLayerfontUIFont,而是CFTypeRef类型
    可以根据需要使用CGFontRef 还是 CTFontRef
  2. fontSize属性需要单独设置,CGFontRefCTFontRef并非UIFont那样可以包含 像素点的大小 。
  3. 字符类型是 id 类型
  • 图层绘制文字
- (void)viewDidLoad {

[super  viewDidLoad];

 CATextLayer *textLayer = [CATextLayer  layer];
textLayer.frame = self.clipView.bounds;
[self.clipView.layer addSublayer:textLayer];

textLayer.foregroundColor = [UIColor  blackColor].CGColor;
textLayer.alignmentMode = kCAAlignmentJustified;
textLayer.wrapped = YES;

UIFont *font = [UIFont systemFontOfSize:15];

CFStringRef fontName = (__bridge  CFStringRef)font.fontName;
CGFontRef fontRef = CGFontCreateWithFontName(fontName);
textLayer.font = fontRef;
textLayer.fontSize = font.pointSize;

 CGFontRelease(fontRef); // 防止CGFontRef为空而Crash

 NSString *str = @"黛安娜永远都佩着她的月刃,她是皎月教派的武士,不过她的教派在巨神峰周围地区几乎已销声匿迹。黛安娜是皎月神力的凡间化身,身穿微光闪烁的铠甲,就像冬夜的飘雪一样发出寒光。它在巨神峰之巅接受了星灵精魄的融合,已经不再是单纯的凡人,现在的她努力抗争着,寻找着神的启示,寻找自己的力量和存在于这个世界的意义。";

textLayer.string = str;

 // 此步 以上显示的文字 略像素化
 // 图层屏幕拉伸应同实际屏幕拉伸相同
textLayer.contentsScale = [UIScreen  mainScreen].scale;

 NSLog(@"-------viewDidlod-------");

}
  • 实现“富文本”

需要在项目中导入 Core Text Framework

- (void)viewDidLoad {
   [super viewDidLoad];
   
   CATextLayer *textLayer = [CATextLayer layer];
   textLayer.frame = self.clipView.bounds;
   textLayer.contentsScale = [UIScreen mainScreen].scale;
   [self.clipView.layer addSublayer:textLayer];
   
   textLayer.alignmentMode = kCAAlignmentJustified;
   textLayer.wrapped = YES;
   UIFont *font = [UIFont systemFontOfSize:15];
   
   NSString *text = @"Ruiwen do not doubt, expect to work hard to succeed. When the soldiers as she showed good potential, although the diminutive but exercise their proficient use of the sword.She was brutally efficient soldiers, and forces from the heart with conviction. When into the battlefield, Riven heart never wavered: disdain moral restraint, no fear of death fear. Therefore Riven stand out from their peers, as the representative Noxian spirit. She has been unusually high command fanatical appreciation Riven get a Rune Sword Black Noxian magic cast by strengthening before. This weapon heavier than the average light shield, perfect fit Riven preferences. Soon, Riven be assigned access to AO Virginia, became part of the Noxian war of aggression.";
   
   NSMutableAttributedString *string = nil;
   string = [[NSMutableAttributedString alloc] initWithString:text];
   
   CFStringRef fontName = (__bridge CFStringRef)font.fontName;
   CGFloat fontSize = font.pointSize;
   CTFontRef fontRef = CTFontCreateWithName(fontName, fontSize, NULL);
   
   NSDictionary *attribs = @{
                             (__bridge id)kCTForegroundColorAttributeName:(__bridge id)[UIColor blackColor].CGColor,
                             (__bridge id)kCTUnderlineStyleAttributeName:@(kCTUnderlineStyleSingle),
                             (__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)];
   
   CFRelease(fontRef);
   
   textLayer.string = string;
}
CoreAnimation----CATextLayer_第2张图片
富文本.png

你可能感兴趣的:(CoreAnimation----CATextLayer)