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;


目录
相关文章
|
JavaScript 前端开发 Java
MVP开发模式
MVP开发模式
659 3
|
存储 JSON 关系型数据库
基于GeoTools的GeoJson导入到PostGis实战
GeoJson是一种对各种地理数据结构进行编码的格式,基于json的地理空间信息数据交换格式。GeoJson对象可以用来表示几何,特征或者特征集合。支持地理点、线、面、多点、多线、多面及几何集合。GeoJson不是本文的重点,因此不再赘述。
2775 0
基于GeoTools的GeoJson导入到PostGis实战
|
5月前
|
传感器 前端开发 API
TikTok多账号运营环境的技术架构与稳定性分析
TikTok多账号运营常因设备指纹、IP关联及行为异常被连带封禁。本文详解其移动优先风控逻辑,指出传统浏览器方案失效原因,并提出基于真实云手机+独立住宅代理+拟人化自动化的一站式解决方案,助你构建稳定、可维护的运营体系。
|
6月前
|
canal 关系型数据库 MySQL
MySQL LIKE查询太慢?手把手搭建Elasticsearch站内搜索
本文详解MySQL模糊搜索性能瓶颈及Elasticsearch全文检索解决方案:剖析`LIKE '%关键词%'`全表扫描原理,对比MySQL全文索引局限,深入讲解倒排索引机制,并实战演示Logstash/Canal数据同步、IK中文分词、高亮搜索等核心环节,助你构建毫秒级站内搜索。(239字)
|
12月前
|
网络协议 安全 数据挖掘
阿里云服务器九代实例c9i/g9i/r9i与c9a/g9a/r9a对比,性能、价格、适用场景对比与选择参考
目前在阿里云的活动中,除了入门级的轻量应用服务器和经济型e实例,主打高性价比的通用算力型实例云服务器之外,对于需要高性能云服务器的用户,可选择活动内的九代云服务器c9i/g9i/r9i实例和c9a/g9a/r9a实例,本文为大家介绍c9i/g9i/r9i实例和c9a/g9a/r9a实例的架构、处理器、适用场景与价格,以供大家对比和选择参考。
1952 9
|
5月前
|
存储 前端开发 NoSQL
高并发场景下的订单幂等性设计:从重复支付到精准扣款的实战
跨境电商订单系统需解决重复提交导致的重复下单问题。本文详解幂等性原理与实践:通过唯一订单号+数据库索引、Redis幂等令牌(含Lua原子校验)、订单号复用及多服务协同等方案,兼顾可靠性与性能,核心在于唯一标识、状态记录与原子操作三要素。(239字)
344 0
|
6月前
|
虚拟化 UED Windows
VMware Workstation 12.5.7 安装教程(Windows vmware-workstation-full-12.5.7详细步骤)
本教程详解VMware Workstation 12.5.7安装全流程:从管理员运行安装包、接受协议、自定义路径,到关闭更新与体验计划、创建快捷方式,再到输入密钥或试用,最后验证版本。步骤清晰,兼顾新手与实用需求。(239字)
|
8月前
|
安全 API 数据中心
米哈游黑产案解析:游戏账号批量注册如何用IP查询识别外挂与多开用户?操作指南
2026年上海徐汇警方摧毁一游戏黑产团伙,10人落网、涉案超200万元。该团伙利用IP池+“全能注册机”批量注册《原神》等账号并外挂养号出售。本文剖析其IP伪装三大破绽(高密度注册、IDC机房IP、地域跳变),详解四步IP风控实操法,强调IP查询已成为游戏反外挂的基础设施。
1737 0
|
12月前
|
SQL 关系型数据库 MySQL
【数据库进阶】为什么你的SQL查询这么慢?索引失效的7个常见场景
本文总结MySQL索引失效的7大常见场景:模糊查询以%开头、索引列参与计算或函数、隐式类型转换、违背最左前缀法则、OR条件使用不当、不等号查询及全表扫描风险,并结合EXPLAIN工具教你如何诊断与优化,提升查询性能。
|
Android开发
Cocos Creator 花了一个月入门经历
一个与人入门算是最笨的那种了吧 分享给大家入门视频教程
1677 0
Cocos Creator 花了一个月入门经历