利用NSAttributedString实现图文混排
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实现图文混排的更多相关文章
- 利用YYLabel 进行图文混排+高度计算
利用YYLabel 进行图文混排+高度计算 1.项目需求: 用一个控件显示图片和文字,并且依据图片和文字动态计算控件的高度. 2.方案: 利用YYLabel控件和属性字符串处理. 注:(在使用YYLa ...
- 用NSAttributedString实现简单的图文混排
iOS7以后,因为TextKit的强大,可以用NSAttributedString很方便的实现图文混排(主要是利用了NSTextAttachment). 关于Textkit的牛逼之处,可以参考objc ...
- ios图文混排
图文混排的形式 1. 富文本形式 2. core Text(文字排版) 3. TextKit 4. UIWebView 一.富文本 我们可以采用attributeString来进行图文混排.例如一个文 ...
- CoreText实现图文混排
CoreText的介绍 Core Text 是基于 iOS 3.2+ 和 OSX 10.5+ 的一种能够对文本格式和文本布局进行精细控制的文本引擎.它良好的结合了 UIKit 和 Core Graph ...
- iOS开发 - 第05篇 - 项目 - 12 - 图文混排
1.首页微博文字处理 对于之前微博项目中首页:微博文字中的用户名.话题.链接等文字须要高亮显示.表情字符串须要显示相应表情. 思路: 1>之前微博中的文字使用NSString,要达到不同文字的高 ...
- Coretext实现图文混排及Gif图片播放
CoreText是iOS3.2推出的一套文字排版和渲染框架,可以实现图文混排,富文本显示等效果. CoreText中的几个重要的概念: CTFont CTFontCollection CTFontD ...
- EditText图文混排
下面就具体说一下我遇到的问题,首先是EditText里面的图文混排问题,这个问题的难点就是三点: 1.怎么插图片 2.怎么保存插入的图片和文字 3.怎么解析回图片和文字 解决: 一.怎么插入图片 在这 ...
- UITextView实现图文混排效果
用UITextView实现图文混排效果的展示,首先要禁用UITextView的编辑功能,将属性editable设置为NO 1.首先创建一个NSTextAttachment对象,这个对象有一个image ...
- CoreText 实现图文混排
CoreText 实现图文混排 相关博文推荐 IOS CoreText.framework - 基本用法 IOS CoreText.framework - 段落样子CTParagraphStyle h ...
随机推荐
- Python基础学习-列表基本操作
列表:Python的“苦力”. 列表不同于元组和字条串的地方:列表是可变的——可以改变列表的内容,并且列表有很多有用的.专门的方法. 1.list函数 因为字符串不能像列表一样被修改,所有有时根 ...
- webpack踩坑
1.当你用webpack2实现css文件单独成一个文件的时候: 可能遇到这种错误Error: Breaking change: extract now only takes a single argu ...
- SQL-常用数据类型
整数除了 INT 外,还有 TINYINT.SMALLINT.MEDIUMINT.BIGINT. CHAR 和 VARCHAR 的区别: CHAR 的长度是固定的,而 VARCHAR 的长度是可以变化 ...
- 初学React:定义一个组件
接着聊React,今天说说如何创建一个组件类. <!DOCTYPE html> <html lang="en"> <head> <meta ...
- 使用Excel消费C4C的OData service
步骤比较简单, 打开Excel的标签Data->From Other Sources->From OData Data Feed: 输入如下url: https://.c4c.saphyb ...
- 将TIMESTAMP类型的差值转化为秒的方法
两个TIMESTAMP之差得到的是INTERVAL类型,而有时我们只需要得到两个时间相差的秒数,如果变成INTERVAL之后,想要获取这个值会非常麻烦. 比较常见的方法是使用EXTRACT来抽取获得的 ...
- 最长公共单词,类似LCS,(POJ2250)
题目链接:http://poj.org/problem?id=2250 解题报告: 1.状态转移方程: ; i<=len1; i++) { ; j<=len2; j++) { dp[i][ ...
- Poj(2236),简单并查集
题目链接:http://poj.org/problem?id=2236 思路很简单,傻逼的我输出写成了FALL,然后遍历的时候for循环写错了,还好很快我就Debug出来了. #include < ...
- 软件架构中的SOA架构有哪些特点?
面向服务的架构(SOA)是一个组件模型,它将应用程序的不同功能单元(称为服务)通过这些服务之间定义良好的接口和契约联系起来.构建在各种各样的系统中的服务可以以一种统一和通用的方式进行交互. SOA是一 ...
- c#转载的
C#做项目时的一些经验分享 1.对于公用的类型定义,要单独抽取出来,放到单独的DLL中. 2.通过大量定义interface接口,来提高模块化程度,不同功能之间通过实现接口来面向接口编程. 3.如果项 ...