UILabel 和 UITextView 都能添加 NSAttributedString 属性字符串,通过这一点,可以实现带有属性的文字和文字内包含图片的文本内容展示.


效果如下: 
 

1-初始化可变属性字符串

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:textString];

2-设置全局字体属性(设置字体大小为14)

[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, textString.length)];

[attributedString addAttribute:NSKernAttributeName value:@1 range:NSMakeRange(0, textString.length)];

上面两句代码可以简写为一句(为属性字符串同时添加多个属性)

[attributedString addAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14],NSKernAttributeName: @1} range:NSMakeRange(0, textString.length)];

3-修改标题文字属性

通过字符串获取范围

[attributedString addAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:26],NSForegroundColorAttributeName: [UIColor blueColor]} range:[textString rangeOfString:@"360云盘服务转型公告"]];

4-获取一大段文字范围并修改属性

通过前后字符串获取大段字符的范围

// 此方法可以通过string获得范围进行修改

NSRange startRange = [textString localizedStandardRangeOfString:@"我们即将采取以下措施:"];

NSRange endRange = [textString localizedStandardRangeOfString:@"感谢您的一路相伴。"];

[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSUnionRange(startRange, endRange)];

5-为文本添加下划线

// 设置文本下划线

NSRange startRange1 = [textString localizedStandardRangeOfString:@"因此,"];

NSRange endRange1 = [textString localizedStandardRangeOfString:@"之后转型企业云服务。"];

[attributedString addAttribute:NSUnderlineStyleAttributeName value:@1 range:NSUnionRange(startRange1, endRange1)];

6-为文本内文字添加描边

// 设置文本的描边

[attributedString addAttribute:NSStrokeWidthAttributeName value:@2.0 range:[textString rangeOfString:@"存储传播非法文件、侵权盗版牟利、传播淫秽色情信息等违法犯罪行为"]];

[attributedString addAttribute:NSStrokeColorAttributeName value:[UIColor brownColor] range:[textString rangeOfString:@"存储传播非法文件、侵权盗版牟利、传播淫秽色情信息等违法犯罪行为"]];

[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:[textString rangeOfString:@"存储传播非法文件、侵权盗版牟利、传播淫秽色情信息等违法犯罪行为"]];

7-为文本添加图片附件

// 插入图片附件

NSTextAttachment *imageAtta = [[NSTextAttachment alloc] init];

imageAtta.bounds = CGRectMake(0, 0, 375, 180);

imageAtta.image = [UIImage imageNamed:@"360"];

NSAttributedString *attach = [NSAttributedString attributedStringWithAttachment:imageAtta];

[attributedString insertAttributedString:attach atIndex:0];

8-为文本设置段落属性

// 段落样式

NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];

// 行间距

[style setLineSpacing:3];

// 段落间距

[style setParagraphSpacing:6];

// 首行缩进

[style setFirstLineHeadIndent:25];

[attributedString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(1, textString.length)];

9-添加网址链接

// 网址链接

NSRange urlRange = [textString rangeOfString:@"yunpan.360.cn"];

[attributedString addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"http://yunpan.360.cn"] range:NSMakeRange(urlRange.location, 14)];

[attributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(urlRange.location, 14)];

10-通过UITextViewDelegate代理方法,监听URL和附件的点击

#pragma mark ----------UITextViewDelegate----------

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction { NSLog(@"%@",URL); return YES; }

- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction { NSLog(@"%@",textAttachment.image); return YES; }

补充:常用属性字符串属性

// 字体 NSFontAttributeName // UIFont, default Helvetica(Neue) 12

// 段落 NSParagraphStyleAttributeName // NSParagraphStyle, default defaultParagraphStyle

// 文字颜色 NSForegroundColorAttributeName // UIColor, default blackColor

// 背景颜色 NSBackgroundColorAttributeName // UIColor, default nil: no background

// 描边颜色 NSStrokeColorAttributeName // UIColor, default nil: same as foreground color

// 描边宽度 NSStrokeWidthAttributeName // NSNumber containing floating point value, default 0

// 阴影 NSShadowAttributeName // NSShadow, default nil: no shadow

// 附件 NSAttachmentAttributeName // NSTextAttachment, default nil

// 链接URL NSLinkAttributeName // NSURL (preferred) or NSString

// 基线偏移量 NSBaselineOffsetAttributeName // NSNumber containing floating point value,default 0

// 下划线 NSUnderlineColorAttributeName // UIColor, default nil: same as foreground color

转自:http://blog.csdn.net/Mazy_ma/article/details/52920596

利用NSAttributedString实现图文混排的更多相关文章

  1. 利用YYLabel 进行图文混排+高度计算

    利用YYLabel 进行图文混排+高度计算 1.项目需求: 用一个控件显示图片和文字,并且依据图片和文字动态计算控件的高度. 2.方案: 利用YYLabel控件和属性字符串处理. 注:(在使用YYLa ...

  2. 用NSAttributedString实现简单的图文混排

    iOS7以后,因为TextKit的强大,可以用NSAttributedString很方便的实现图文混排(主要是利用了NSTextAttachment). 关于Textkit的牛逼之处,可以参考objc ...

  3. ios图文混排

    图文混排的形式 1. 富文本形式 2. core Text(文字排版) 3. TextKit 4. UIWebView 一.富文本 我们可以采用attributeString来进行图文混排.例如一个文 ...

  4. CoreText实现图文混排

    CoreText的介绍 Core Text 是基于 iOS 3.2+ 和 OSX 10.5+ 的一种能够对文本格式和文本布局进行精细控制的文本引擎.它良好的结合了 UIKit 和 Core Graph ...

  5. iOS开发 - 第05篇 - 项目 - 12 - 图文混排

    1.首页微博文字处理 对于之前微博项目中首页:微博文字中的用户名.话题.链接等文字须要高亮显示.表情字符串须要显示相应表情. 思路: 1>之前微博中的文字使用NSString,要达到不同文字的高 ...

  6. Coretext实现图文混排及Gif图片播放

    CoreText是iOS3.2推出的一套文字排版和渲染框架,可以实现图文混排,富文本显示等效果. CoreText中的几个重要的概念:  CTFont CTFontCollection CTFontD ...

  7. EditText图文混排

    下面就具体说一下我遇到的问题,首先是EditText里面的图文混排问题,这个问题的难点就是三点: 1.怎么插图片 2.怎么保存插入的图片和文字 3.怎么解析回图片和文字 解决: 一.怎么插入图片 在这 ...

  8. UITextView实现图文混排效果

    用UITextView实现图文混排效果的展示,首先要禁用UITextView的编辑功能,将属性editable设置为NO 1.首先创建一个NSTextAttachment对象,这个对象有一个image ...

  9. CoreText 实现图文混排

    CoreText 实现图文混排 相关博文推荐 IOS CoreText.framework - 基本用法 IOS CoreText.framework - 段落样子CTParagraphStyle h ...

随机推荐

  1. canvas 绘制八卦图

    绘制要点: 1.getContext('2d'); -->绘图环境,2维空间 2.fillRect(x,y,w,h); -->矩形:实心(黑色背景) 3.strokeRect(x,y,w, ...

  2. MySQL数据库实验三:连接查询

    实验三    连接查询 实验名称:连接查询(2课时) 一.实验目的 理解JOIN语句的操作和基本使用方法,掌握内连接.外连接.自身连接的概念和使用. 二.实验环境 是MS SQL SERVER 200 ...

  3. MYSQL忘记超级用户密码修改

    #service mysql stop #mysqld_safe --skip-grant-tables 另外开个SSH连接或终端 [root@localhost ~]# mysql mysql> ...

  4. POJ-2456 Aggressive cows---最大化最小值(也就是求最大值)

    题目链接: https://vjudge.net/problem/POJ-2456 题目大意: 有n个牛栏,选m个放进牛,相当于一条线段上有 n 个点,选取 m 个点, 使得相邻点之间的最小距离值最大 ...

  5. 进程管理—进程描述符(task_struct)

    http://blog.csdn.net/qq_26768741/article/details/54348586 当把一个程序加载到内存当中,此时,这个时候就有了进程,关于进程,有一个相关的叫做进程 ...

  6. 2017.9.17 HTML学习总结---table标签

    接上: 2.1.3  HTML表单标签与表单设计 表单是用户与服务器交互的主要方法,用户在表单中输入数据,提交给服务器程序来处理. (1)表单的组成: 文本框(text),密码框(password), ...

  7. Java解析Excel工具类(兼容xls和xlsx)

    依赖jar <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml&l ...

  8. numpy中生成随机矩阵并打印出矩阵的shape

    from numpy import * c=zeros((4,5)) print c.shape print numpy.random.random((2,3))

  9. iterm2配置项

    1. 启动终端Terminal2. 进入当前用户的home目录    输入cd ~3. 创建.bash_profile    输入touch .bash_profile 在导入并应用完颜色方案之后,通 ...

  10. java对象传递小解析

    先上代码: import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder ...