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. April 22 2017 Week 16 Saturday

    Fear is an essential part of our survival, it keeps us alert. 恐惧是生存的重要部分,它让我们保持警惕. Fear and pain are ...

  2. 关于定义顺序和内存分配的关系--记一道不严谨的C语言题

    include<stdio.h> #include<iostream> int main() { char a[] = "123"; char b[] = ...

  3. 323. Number of Connected Components in an Undirected Graph (leetcode)

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  4. 利用python的numpy创建矩阵并对其赋值

    创建一个3X3的矩阵并对其赋值: x = numpy.array([[1,2,3],[4,5,6],[7,8,9]]) print x print x.shape 运行结果: [[ ] [ ] [ ] ...

  5. getnumdevices.c && setgetdevicetype例程

    getnumdevices.c代码 /* 文件名: getnumdevices.c * 功能 : 测试函数acc_get_num_devices(.) */ #include<stdio.h&g ...

  6. rnn,cnn

    http://nikhilbuduma.com/2015/01/11/a-deep-dive-into-recurrent-neural-networks/ 按照这里的介绍,目前比较火的cnn是fee ...

  7. 《阿里巴巴Java开发手册》阅读笔记

    1.抽象类命名使用 Abstract 或 Base 开头: 异常类命名使用 Exception 结尾: 测试类命名以它要测试的类的名称开始,以 Test 结尾. 2.POJO 类中布尔类型的变量,都不 ...

  8. 【主席树上二分】bzoj5361: [Lydsy1805月赛]对称数

    随机化选讲例题 题目大意 小 Q 认为,偶数具有对称美,而奇数则没有.给定一棵 n 个点的树,任意两点之间有且仅有一条直接或间接路径.这些点编号依次为 1 到 n,其中编号为 i 的点上有一个正整数 ...

  9. JAVA实现RSA加密,非对称加密算法

    RSA.java package org.icesnow.jeasywx.util.security; import java.security.Key; import java.security.K ...

  10. 【c学习-1】

    #include<stdio.h> int main(){ int a,b,max; printf("请输入两个整数:"); //格式化输出函数 scanf(" ...