欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS 中CATextLayer绘制文本字符串

程序员文章站 2023-12-18 10:22:40
ios 中catextlayer绘制文本字符串 catextlayer使用core text进行绘制,渲染速度比使用web kit的uilable快很多。而且uilabl...

ios 中catextlayer绘制文本字符串

catextlayer使用core text进行绘制,渲染速度比使用web kit的uilable快很多。而且uilable主要是管理内容,而catextlayer则是绘制内容。

catextlayer的绘制文本字符串的效果如下:

IOS 中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; 

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:

下一篇: