CATextLayer绘制文本字符串
2017-02-21 17:43
288 查看
CATextLayer使用Core Text进行绘制,渲染速度比使用Web Kit的UILable快很多。而且UILable主要是管理内容,而CATextLayer则是绘制内容。
CATextLayer的绘制文本字符串的效果如下:
![](http://img.blog.csdn.net/20170221173630601?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcG90YXRvNTEy/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
代码示例:
// 绘制文本的图层
CATextLayer *layerText = [[CATextLayer alloc] init];
// 背景颜色
layerText.backgroundColor = [UIColor orangeColor].CGColor;
// 渲染分辨率-重要,否则显示模糊
layerText.contentsScale = [UIScreen mainScreen].scale;
// 显示位置
layerText.bounds = CGRectMake(0.0, 0.0, 100.0, 50.0);
layerText.position = position;
// 添加到父图书
[self.view.layer addSublayer:layerText];
CATextLayer的绘制文本字符串的效果如下:
代码示例:
// 绘制文本的图层
CATextLayer *layerText = [[CATextLayer alloc] init];
// 背景颜色
layerText.backgroundColor = [UIColor orangeColor].CGColor;
// 渲染分辨率-重要,否则显示模糊
layerText.contentsScale = [UIScreen mainScreen].scale;
// 显示位置
layerText.bounds = CGRectMake(0.0, 0.0, 100.0, 50.0);
layerText.position = position;
// 添加到父图书
[self.view.layer addSublayer:layerText];
// 分行显示 layerText.wrapped = YES; // 超长显示时,省略号位置 layerText.truncationMode = kCATruncationNone; // 字体颜色 layerText.foregroundColor = [UIColor purpleColor].CGColor; // 字体名称、大小 UIFont *font = [UIFont systemFontOfSize:15.0]; CFStringRef fontName = (__bridge CFStringRef)font.fontName; CGFontRef fontRef =CGFontCreateWithFontName(fontName); layerText.font = fontRef; layerText.fontSize = font.pointSize; CGFontRelease(fontRef); // 字体对方方式 layerText.alignmentMode = kCAAlignmentJustified;
// 字符显示 NSString *text = @"devZhang is iOSDeveloper.devZhang is iOSDeveloper.devZhang is iOSDeveloper.devZhang is iOSDeveloper.";
// 方法1-简单显示 layerText.string = text;
// 方法2-富文本显示 // 文本段落样式 NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle alloc] init]; textStyle.lineBreakMode = NSLineBreakByWordWrapping; // 结尾部分的内容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz") textStyle.alignment = NSTextAlignmentCenter; //(两端对齐的)文本对齐方式:(左,中,右,两端对齐,自然) textStyle.lineSpacing = 5; // 字体的行间距 textStyle.firstLineHeadIndent = 5.0; // 首行缩进 textStyle.headIndent = 0.0; // 整体缩进(首行除外) textStyle.tailIndent = 0.0; // textStyle.minimumLineHeight = 20.0; // 最低行高 textStyle.maximumLineHeight = 20.0; // 最大行高 textStyle.paragraphSpacing = 15; // 段与段之间的间距 textStyle.paragraphSpacingBefore = 22.0f; // 段首行空白空间/* Distance between the bottom of the previous paragraph (or the end of its paragraphSpacing, if any) and the top of this paragraph. */ textStyle.baseWritingDirection = NSWritingDirectionLeftToRight; // 从左到右的书写方向(一共➡️三种) textStyle.lineHeightMultiple = 15; /* Natural line height is multiplied by this factor (if positive) before being constrained by minimum and maximum line height. */ textStyle.hyphenationFactor = 1; //连字属性 在iOS,唯一支持的值分别为0和1 // 文本属性 NSMutableDictionary *textAttributes = [[NSMutableDictionary alloc] init]; // NSParagraphStyleAttributeName 段落样式 [textAttributes setValue:textStyle forKey:NSParagraphStyleAttributeName]; // NSFontAttributeName 字体名称和大小 [textAttributes setValue:[UIFont systemFontOfSize:12.0] forKey:NSFontAttributeName]; // NSForegroundColorAttributeNam 颜色 [textAttributes setValue:[UIColor greenColor] forKey:NSForegroundColorAttributeName]; NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text]; [attributedText setAttributes:textAttributes range:NSMakeRange(0, 8)]; layerText.string = attributedText;
相关文章推荐
- cvPutText:在图像上绘制文本字符串
- IOS 中CATextLayer绘制文本字符串
- CoreAnimation —— CATextLayer(文本绘制)
- js的base64和文本字符串之间互转
- C++ boost 组件简介:字符串及文本处理
- C# 测量用指定的 Font 绘制的指定字符串。(指定字符串的像素长度)
- shell在文本第一行和最后一行添加字符串
- WinForm绘制文本--字体加粗、倾斜、下划线
- 正则表达式实现将html文本转换为纯文本格式(将html字符串转换为纯文本方法)
- Swing中Graphics绘制图片,图片使用随机背景色,随机字符串
- 轻松python文本专题-单独处理字符串每个字符的方法汇总
- iOS UIlabel怎么加载html字符串 富文本的用法
- KMP算法 --- 在文本中寻找目标字符串
- OSG绘制大量文本的解决方法:文本绘制的纹理活字印刷术
- svg沿路径绘制文本
- python cookbook 字符串和文本
- 关于指针,字符串,数据库文本默认值
- c# GDI+绘制不同字体的字符串
- 「切」slice() //取字符串的片断,-1 指字符串的最后一个字符,-2 指倒数第二个字符 20140821 ①文本处理
- WinAPI: DrawText - 将文本绘制到指定的矩形中