CoreAnimation----CATextLayer

CATextLayer


大美女
大美女

1. CATextLayer

  • UILabel的精髓

在一個圖層里面顯示文字,可以借助圖層代理直接將字符串用Core Graphics寫到圖層的內容里

  • CALayer的一個子類

以圖層形式包含UILabel幾乎所有的繪制的特性,并且提供了額外的一些新的特性 。

  • CATextLayer的優(yōu)點

相較于UILabel渲染的速度更快
iOS6以及之前版本UILabel是通過WebKit進行渲染(文字一多,性能壓力極大)
CATextLayer通過Core Text,渲染極快


CoreText是Mac OS和iOS系統(tǒng)中處理文本的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 = @"黛安娜永遠都佩著她的月刃,她是皎月教派的武士,不過她的教派在巨神峰周圍地區(qū)幾乎已銷聲匿跡。黛安娜是皎月神力的凡間化身,身穿微光閃爍的鎧甲,就像冬夜的飄雪一樣發(fā)出寒光。它在巨神峰之巔接受了星靈精魄的融合,已經不再是單純的凡人,現在的她努力抗爭著,尋找著神的啟示,尋找自己的力量和存在于這個世界的意義。";

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;
}
富文本.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容