CATextLayer

作者: 莫须有恋 | 来源:发表于2016-12-28 16:30 被阅读706次
部分属性和方法 介绍
string NSString或者NSAttributedString
font 使用的字体,可以是CTFontRef, CGFontRef或者是一个font的名字,string非NSAttributedString时使用
fontSize 字体大小,默认36,string非NSAttributedString时使用,支持动画
foregroundColor 字体颜色,默认白色,string非NSAttributedString时使用,支持动画
wrapped 自动换行,默认NO
truncationMode 当文本显示不全时的裁剪方式kCATruncationNone不剪裁,默认;kCATruncationStart剪裁开始部分;kCATruncationEnd剪裁结束部分;kCATruncationMiddle剪裁中间部分;
alignmentMode 对齐方式,kCAAlignmentNatural默认,其他还有kCAAlignmentLeft左对齐;kCAAlignmentRight右对齐;kCAAlignmentCenter居中;kCAAlignmentJustified两端对齐
这两个CTFontRef,CGFontRef没用过,参考其他文章使用吧
Demo部分代码
    //设置渲染的方式
    layer.contentsScale = [UIScreen mainScreen].scale;
    
    //如果没有设置其他样式的情况下,使用下边的代码能让我计算的高度准确一点
    //有办法计算高度准确的,请一定要联系我告诉我。。
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    paragraph.lineSpacing = 1;
    
    //字体颜色
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSForegroundColorAttributeName:[UIColor redColor],NSParagraphStyleAttributeName:paragraph}];
    //字体大小
    [attributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:19] range:NSMakeRange(0, str.length)];
    
    layer.string = attributedStr;
    //计算字体高度
    layer.bounds = [attributedStr boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
    //换行
    layer.wrapped = YES;
    //裁剪方式
    layer.truncationMode = kCATruncationEnd;
    //对齐
    layer.alignmentMode = kCAAlignmentNatural;
Demo效果图
接上一篇CAShapeLayer实现Demo效果
结合[CAShapeLayer](http://www.jianshu.com/p/c57c15ec59c7)实现Demo效果
部分代码
    //底部红色字体图层
    CATextLayer *text1 = [self attributeLayer:@"50%" width:layer_width textColor:[UIColor redColor]];
    text1.position = self.view.center;
    [self.view.layer addSublayer:text1];
    
    
    //文字和水波的图层
    CALayer *mainLayer = [CALayer layer];
    mainLayer.backgroundColor = [UIColor redColor].CGColor;
    mainLayer.bounds = CGRectMake(0, 0, layer_width, layer_width);
    mainLayer.position = self.view.center;
    mainLayer.mask = _waterLayer;
    
    CATextLayer *text2 = [self attributeLayer:@"50%" width:layer_width textColor:[UIColor whiteColor]];
    text2.position = [mainLayer convertPoint:self.view.center fromLayer:self.view.layer];
    [mainLayer addSublayer:text2];
    
    [self.view.layer addSublayer:mainLayer];
记录一下搜索到的好文

Core Text 入门

//这里记录一下富文本一些属性表示的意思

// NSFontAttributeName 设置字体属性,默认值:字体:Helvetica(Neue) 字号:12
// NSForegroundColorAttributeNam 设置字体颜色,取值为 UIColor对象,默认值为黑色
// NSBackgroundColorAttributeName 设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色
// NSLigatureAttributeName 设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符
// NSKernAttributeName 设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
// NSStrikethroughStyleAttributeName 设置删除线,取值为 NSNumber 对象(整数)
// NSStrikethroughColorAttributeName 设置删除线颜色,取值为 UIColor 对象,默认值为黑色
// NSUnderlineStyleAttributeName 设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似
// NSUnderlineColorAttributeName 设置下划线颜色,取值为 UIColor 对象,默认值为黑色
// NSStrokeWidthAttributeName 设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
// NSStrokeColorAttributeName 填充部分颜色,不是字体颜色,取值为 UIColor 对象
// NSShadowAttributeName 设置阴影属性,取值为 NSShadow 对象
// NSTextEffectAttributeName 设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用
// NSBaselineOffsetAttributeName 设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
// NSObliquenessAttributeName 设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
// NSExpansionAttributeName 设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
// NSWritingDirectionAttributeName 设置文字书写方向,从左向右书写或者从右向左书写
// NSVerticalGlyphFormAttributeName 设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
// NSLinkAttributeName 设置链接属性,点击后调用浏览器打开指定URL地址
// NSAttachmentAttributeName 设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
// NSParagraphStyleAttributeName 设置文本段落排版格式,取值为 NSParagraphStyle 对象

相关文章

  • iOS[QuartzCore框架](CATextLayer)(6

    一、CATextLayer简介 CATextLayer可以通过字符串进行文字的绘制。 二、CATextLayer....

  • QuartzCore - CATextLayer

    CATextLayer 垂直居中显示,自定义 CATextLayer ,重写 draw(in ctx:) 方法

  • 2018-01-30

    CATextLayer 渲染速度 快鱼 UILabel所以以后争取用 CATextLayer 代替UILabel的...

  • CoreAnimation----CATextLayer

    CATextLayer 1. CATextLayer UILabel的精髓 在一个图层里面显示文字,可以借助图层代...

  • CATextLayer

    这两个CTFontRef,CGFontRef没用过,参考其他文章使用吧 Demo部分代码 接上一篇CAShapeL...

  • CATextLayer

    在界面上添加文本有两种;一种是: UILabel;第二种是:CATextLayer;最近在改到一个菜单的demo ...

  • CATextLayer

    在界面上添加文本有两种;一种是: UILabel;第二种是:CATextLayer;最近在改到一个菜单的demo ...

  • CATextLayer

    CATextLayer也要比UILabel渲染得快得多。 很少有人知道在iOS 6及之前的版本,UILabel其实...

  • CoreAnimation之CATextLayer

    CATextLayer是专用的文本显示图层,有和UILaber一样的显示功能,只是CATextLayer的文本渲染...

  • 第8章 专用图层

    8.2 CATextLayer UILabel的替代品: 这应该是一个用CATextLayer作为宿主图层的UIL...

网友评论

    本文标题:CATextLayer

    本文链接:https://www.haomeiwen.com/subject/rlqyvttx.html