ios 中catextlayer绘制文本字符串
catextlayer使用core text进行绘制,渲染速度比使用web kit的uilable快很多。而且uilable主要是管理内容,而catextlayer则是绘制内容。
catextlayer的绘制文本字符串的效果如下:
代码示例:
1
2
3
4
5
6
7
8
9
10
11
|
// 绘制文本的图层 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]; |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
// 分行显示 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; |
1
2
|
// 字符显示 nsstring *text = @ "devzhang is iosdeveloper.devzhang is iosdeveloper.devzhang is iosdeveloper.devzhang is iosdeveloper." ; |
1
2
|
// 方法1-简单显示 layertext.string = text; |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
// 方法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; |
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:http://blog.csdn.net/potato512/article/details/56290428