利用CATextLayer渲染文本
来源:互联网 发布:球球大作战java源代码 编辑:程序博客网 时间:2024/05/17 05:10
本文总结自 iOS核心动画总结
该系列更多文章点击
#import "ViewController.h"#import <CoreText/CoreText.h>@interface ViewController ()@property (weak, nonatomic) IBOutlet UIView *labelView;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad];// [self showSomeText]; [self showSomeTextWithAttributr];}- (void)showSomeTextWithAttributr { CATextLayer *textLayer = [CATextLayer layer]; textLayer.frame = _labelView.bounds; textLayer.contentsScale = [UIScreen mainScreen].scale; [_labelView.layer addSublayer:textLayer]; textLayer.alignmentMode = kCAAlignmentJustified; textLayer.wrapped = YES; UIFont *font = [UIFont systemFontOfSize:20]; NSString *text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque massa arcu, eleifend vel varius in, facilisis pulvinar leo. Nunc quis nunc at mauris pharetra condimentum ut ac neque. Nuncelementum, libero ut porttitor dictum, diam odio congue lacus, velfringilla sapien diam at purus. Etiam suscipit pretium nunc sit amet lobortis"; NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:text]; CFStringRef fontName = (__bridge CFStringRef)font.fontName; CGFloat fontSize = font.pointSize; CTFontRef fontRef = CTFontCreateWithName(fontName, fontSize, NULL);// CT NSDictionary *attr = @{(__bridge id)kCTForegroundColorAttributeName : (__bridge id)[UIColor redColor].CGColor, (__bridge id)kCTFontAttributeName : (__bridge id) fontRef}; [string setAttributes:attr range:NSMakeRange(0, text.length)]; attr = @{(__bridge id)kCTForegroundColorAttributeName : (__bridge id)[UIColor yellowColor].CGColor, (__bridge id)kCTFontAttributeName : (__bridge id)fontRef, (__bridge id)kCTUnderlineStyleAttributeName : @(kCTUnderlineStyleThick)}; [string setAttributes:attr range:NSMakeRange(4, 40)]; CFRelease(fontRef); textLayer.string = string;}/// 利用layer显示文字,基本的- (void)showSomeText{ CATextLayer *textLayer = [CATextLayer layer]; textLayer.frame = _labelView.bounds; [self.labelView.layer addSublayer:textLayer]; textLayer.foregroundColor = [UIColor blueColor].CGColor; textLayer.alignmentMode = kCAAlignmentJustified; textLayer.truncationMode = kCATruncationEnd; textLayer.wrapped = YES; // 换行 textLayer.contentsScale = [UIScreen mainScreen].scale;// 这个设置让根据当前屏幕进行渲染,防止像素化 UIFont *font = [UIFont systemFontOfSize:15]; CFStringRef fontName = (__bridge CFStringRef)font.fontName; CGFontRef fontRef = CGFontCreateWithFontName(fontName); // CF 这个属性可以是coreText的 textLayer.font = fontRef; textLayer.fontSize = font.pointSize; CGFontRelease(fontRef); NSString *text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque massa arcu, eleifend vel varius in, facilisis pulvinar leo. Nunc quis nunc at mauris pharetra condimentum ut ac neque. Nunc elementum, libero ut porttitor dictum, diam odio congue lacus, vel fringilla sapien diam at purus. Etiam suscipit pretium nunc sit amet lobortis"; textLayer.string = text;}
文章中有部分的需要解释的稍后在编辑,有什么疑问也可以在评论留言
demo
0 0
- 利用CATextLayer渲染文本
- iOS CATextLayer 富文本
- CATextLayer绘制文本字符串
- CATextLayer快速更改文本
- CATextLayer
- CATextLayer
- Core Animation - CATextLayer和富文本
- Cocos2d-x 文本渲染
- 高质量文本渲染
- text-rendering文本渲染
- 使用CATextLayer
- CATextLayer使用
- CATextLayer类
- 利用CATextLayer和NSMutableAttributedString可以使字符串中的每个字颜色不同
- CEGUI文本渲染(一)
- CEGUI文本渲染(二)
- Cocos2d-iphone 文本渲染系统
- CATextLayer字体模糊
- 在使用lua的tolua的内存泄露注意
- JavaScript基本概念(一)
- A new Chapter----AFO
- Linear Algebra - Lesson 16. 投影矩阵和最小二乘
- [game]《泰坦陨落2》
- 利用CATextLayer渲染文本
- JSON(二)Gson的使用
- nginx的gzip压缩功能参数介绍(ngx_http_gzip_module)
- hive 三种启动方式及用途
- 使用Python分析谷歌浏览器Chrome的历史记录
- android中的内部存储与外部存储
- 【Qt开发】 QT:make: Nothing to be done for `first'和error:QtSql:No such file or directory
- 2016年下半年规划
- FIFO管道写规则