IOS 中CATextLayer绘制文本字符串

 更新时间:2017年10月11日 10:36:55   作者:番薯大佬  
这篇文章主要介绍了IOS 中CATextLayer绘制文本字符串的相关资料,希望通过本能帮助到大家,需要的朋友可以参考下

IOS 中CATextLayer绘制文本字符串

CATextLayer使用Core Text进行绘制,渲染速度比使用Web Kit的UILable快很多。而且UILable主要是管理内容,而CATextLayer则是绘制内容。

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; 

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

相关文章

  • Objective-C中block循环引用问题详解

    Objective-C中block循环引用问题详解

    这篇文章主要给大家介绍了关于Objective-C中block循环引用问题的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Objective-C具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-06-06
  • 利用iOS动画来模拟音量振动条的实现

    利用iOS动画来模拟音量振动条的实现

    本篇文章主要利用iOS动画来模拟音量振动条的实现以及对CAReplicatorLayer的简单介绍,需要的朋友可以参考下
    2015-07-07
  • iOS开发教程之APP内部切换语言的实现方法

    iOS开发教程之APP内部切换语言的实现方法

    这篇文章主要给大家介绍了关于iOS开发教程之APP内部切换语言的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-02-02
  • iOS 断点上传文件的实现方法

    iOS 断点上传文件的实现方法

    这项目开发中,有时候我们需要将本地的文件上传到服务器,简单的几张图片还好,但是针对iPhone里面的视频文件进行上传,为了用户体验,我们有必要实现断点上传。这篇文章主要介绍了iOS 断点上传文件的实现方法,需要的朋友可以参考下
    2017-12-12
  • ios中图像进行压缩方法汇总

    ios中图像进行压缩方法汇总

    在Iphone上有两种读取图片数据的简单方法: UIImageJPEGRepresentation和UIImagePNGRepresentation. UIImageJPEGRepresentation函数需要两个参数:图片的引用和压缩系数.而UIImagePNGRepresentation只需要图片引用作为参数.
    2015-05-05
  • iOS实现联系人按照首字母进行排序的实例

    iOS实现联系人按照首字母进行排序的实例

    下面小编就为大家分享一篇iOS实现联系人按照首字母进行排序的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-12-12
  • iOS 项目中的version和build 详解

    iOS 项目中的version和build 详解

    这篇文章主要介绍了iOS 项目中的version和build 详解的相关资料,需要的朋友可以参考下
    2016-11-11
  • React Native学习教程之自定义NavigationBar详解

    React Native学习教程之自定义NavigationBar详解

    这篇文章主要给大家介绍了关于React Native学习教程之自定义NavigationBar的相关资料,文中通过是示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-10-10
  • IOS实现基于CMPedometer的计步器

    IOS实现基于CMPedometer的计步器

    这篇文章主要为大家详细介绍了IOS实现基于CMPedometer的计步器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • iOS自定义相机实现拍照、录制视频

    iOS自定义相机实现拍照、录制视频

    这篇文章主要为大家详细介绍了iOS自定义相机实现拍照、录制视频,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-04-04

最新评论