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. oracle11g在CentOS6.9上启动脚本

    #!/bin/bash# chkconfig:2345 99 10# description: Startup Script for oracle Database# /etc/init.d/orac ...

  2. POJ-3614 Sunscreen---贪心+优先队列

    题目链接: https://vjudge.net/problem/POJ-3614 题目大意: 有C个奶牛去晒太阳 (1 <=C <= 2500),每个奶牛各自能够忍受的阳光强度有一个最小 ...

  3. ios 逆向工程文档汇总

    iOS逆向工程工具集 http://www.jianshu.com/p/7f9511d48e05 移动App入侵与逆向破解技术-iOS篇 http://blog.csdn.net/heiby/arti ...

  4. Poj(1521),哈夫曼编码

    题目链接:http://poj.org/problem?id=1521 这里,网上有很多博客都有写,很多人没有建树,直接就是求一下这个哈夫曼编码的长度,的确很巧妙,我也用的这个方法,但是,几乎所有博客 ...

  5. 有权并查集,Poj(1988)

    题目链接:http://poj.org/problem?id=1988 题目大意: 有n个从1到n编号的箱子,将每个箱子当做一个栈,对这些箱子进行p次操作,每次操作分别为以下两种之一: 输入 M x ...

  6. 2018.7.6 js实现点击事件---点击小图出现大图---时间定时器----注册表单验证

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  7. git常用命令图解

  8. PNChart,简洁高效有动画效果的iOS图表库

    导入 pod导入相对简单,要手动导入这个库,先下载下来(https://github.com/kevinzhow/PNChart),解压后把PNChart文件夹拖入工程中 运行发现#import&qu ...

  9. 【读书笔记】你不知道的JavaScript(上卷)--作用域是什么

    第一章 作用域 1.理解作用域 几个名词的介绍 引擎:从头到尾负责整个JavaScript程序的编译及执行过程 编译器:负责语法分析及代码生成器等脏活累活 作用域:负责收集并维护由所有声明的标识符(变 ...

  10. js、jquery中全局替换replace

    str.replace(/需要替换的/g,"新字符串") //此处使用正则表达式