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;


目录
相关文章
|
Dart 测试技术 开发工具
了解 Flutter 的 Timer 类和 Timer.periodic【Flutter 专题 19】
在构建移动应用程序时,我们经常会遇到必须在一定时间后执行任务的场景。还记得在进入应用程序之前看到闪亮的启动画面吗? 或者我们可能需要一段代码在一段时间后重复执行,比如显示剩余时间限制以填充一次性密码或每秒更改小部件的颜色以创建漂亮的动画。
5864 0
|
Linux 应用服务中间件 nginx
|
存储 分布式计算 Hadoop
分布式计算框架在大规模数据处理中的应用
【8月更文第18天】随着大数据时代的到来,对海量数据进行有效的存储、处理和分析变得越来越重要。传统的单机系统已经无法满足PB级别数据集的需求。分布式计算框架,如Apache Hadoop和Apache Spark,成为了处理这些大规模数据集的重要工具。
930 0
|
存储 设计模式 安全
探索设计模式的魅力:备忘录模式揭秘-实现时光回溯、一键还原、后悔药、历史的守护者和穿越时空隧道
备忘录模式是一种行为设计模式,允许在不破坏对象封装性的情况下保存和恢复对象的内部状态。该模式通过创建备忘录对象来存储发起人的状态信息,发起人可根据需要创建和恢复备忘录。管理者则负责保存和管理备忘录,但无法访问其内容。备忘录模式简化了状态管理,支持撤销操作和历史记录功能,提高了系统的灵活性和可用性。在实际应用中,备忘录模式常用于文本编辑器、游戏和数据库事务处理等场景,确保对象状态的安全恢复和有效管理。通过备忘录模式,开发人员可以更好地控制对象状态的变化,提升软件系统的健壮性和用户体验。
302 1
探索设计模式的魅力:备忘录模式揭秘-实现时光回溯、一键还原、后悔药、历史的守护者和穿越时空隧道
|
算法框架/工具 PyTorch 调度
PyTorch 2.2 中文官方教程(十五)(4)
PyTorch 2.2 中文官方教程(十五)
256 1
PyTorch 2.2 中文官方教程(十五)(4)
|
人工智能 城市大脑 运维
阿里云官网政企业务频道上线!
阿里云官网政企业务频道上线!
345 0
|
编解码 定位技术
【工具分享】如何批量获取图片详细信息,如何把图片的属性信息包括文件路径、文件名、面积尺寸、创建日期、修改日期、水平垂直分辨率、文件大小等图片信息批量提取exel表格中
本文介绍了如何批量提取图片的属性数据到Excel,包括文件名、尺寸、面积、分辨率、GPS信息、创建和修改日期。适合需要处理大量图片信息的工作场景。支持批量导入图片,一键提取各项信息,并能直接导出为表格。
2165 0
|
Android开发
ImageView设置tint ,修改图标颜色
ImageView设置tint ,修改图标颜色
|
Swift iOS开发
UISlider自定义滑动条高度(swift)
UISlider滑动条的高度系统默认给我们设定了,没有办法进行改变。可是,我们的设计又是需要改变这个高度的,怎么办呢?这个时候,我们可以写一个UISlider的子类,重写四个方法就能实现了。
3787 0
|
JavaScript 关系型数据库 MySQL
如何在 Node.js 中连接 MySQL 数据库
如何在 Node.js 中连接 MySQL 数据库
1059 0