iOS中NSAttributedString的使用--对关键字着色,以及处理html实例
1,最近项目中用到了一个功能,一个很好的功能。就是用户在搜索的时候,搜索结果出来后对你输入的关键字进行红色标记。这样用户就很请楚的看到自己输入什么后会出现什么样子的结果。还有一个功能是,现在有一段文字了,但是要对其中的某些字符串进行着色处理,这个时候NSAttibutedString起到了非常大的作用。以下是我写好的一段代码,各位可以拿去用,非常方便的处理好。
#import <Foundation/Foundation.h> @interface NSAttributedString (Color)
/**
* 对内容中部份关键字进行着色处理
*
* @param content 所有内容
* @param searchs 关键字数组
*
* @return
*/
+(NSAttributedString *)attributeStringWithContent:(NSString *)content keyWords:(NSArray *)keyWords;
@end
+(NSAttributedString *)attributeStringWithContent:(NSString *)content keyWords:(NSArray *)keyWords
{
UIColor *color=[UIColor redColor];
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:content];
if (keyWords) {
[keyWords enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSMutableString *tmpString=[NSMutableString stringWithString:content];
NSRange range=[content rangeOfString:obj];
NSInteger location=0;
while (range.length>0) {
[attString addAttribute:(NSString*)NSForegroundColorAttributeName value:color range:NSMakeRange(location+range.location, range.length)];
location+=(range.location+range.length);
NSString *tmp= [tmpString substringWithRange:NSMakeRange(range.location+range.length, content.length-location)];
tmpString=[NSMutableString stringWithString:tmp];
range=[tmp rangeOfString:obj];
}
}];
}
return attString;
}
之前也有看到类似的第三方,他是指写位置,哪个位置到哪个位置的,这里的话是直接指定字符串,而且,出现多次的,一样可以全部着色。大家可以根据自身需求进行细微的调整。
2,还有一种情况,当后台返回的内容中带有html独有的标签怎么办?当然,你可以选择一些第三方来处理,但是对比你想要的功能后,可以发现,这并不是你想要的,太笨重了,我只是要把html标签去了,或是就按html里面的格式显示 。但是又不想用webview去显示 ,因 为webview不能对这些文本进行编辑。这里我就简单写个把html转成NSAttibutedString的方法。
+(NSAttributedString *)attributedStringWithHtml:(NSString *)html
{
NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
NSMutableAttributedString *attrString=[[NSMutableAttributedString alloc] initWithData:[html dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:YES] options:options documentAttributes:nil error:nil];
return attrString; }
这里返回的是按照原来的html格式,CSS样式一样生效的。如果你只想把html标签去了,而不要显示成有CSS样式的,你可以直接在返回的attrString中再调用[attrString string]方法就可以,然后直接显示在label或者textView在,非常的方便。
3.这两天有朋友提到一个需求是,关键字和颜色都自定义,最后写了个方法实现了这个功能。
/**
* 对指定内定进行着色,keywords数组与colors数组相对应
*
* @param content 全部内容
* @param keyWords 关键字数组
* @param color 关键字对应颜色,如果传空,则默认对关键字着红色
* @param repeat 关键字出现多次的时候,是否全部进行多次着色,默认否
*
* @return
*/
+(NSAttributedString *)attributeStringWithContent:(NSString *)content keyWords:(NSArray *)keyWords colors:(NSArray *)colors repeat:(BOOL)repeat;
+(NSAttributedString *)attributeStringWithContent:(NSString *)content keyWords:(NSArray *)keyWords colors:(NSArray *)colors repeat:(BOOL)repeat
{ NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:content];
if (keyWords) { [keyWords enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSMutableString *tmpString=[NSMutableString stringWithString:content];
NSRange range=[content rangeOfString:obj];
NSInteger location=0;
while (range.length>0) {
UIColor *color=nil;
if (!colors[idx]) {
color=[UIColor redColor];
}else{
color=colors[idx];
}
[attString addAttribute:(NSString*)NSForegroundColorAttributeName value:color range:NSMakeRange(location+range.location, range.length)];
location+=(range.location+range.length);
NSString *tmp= [tmpString substringWithRange:NSMakeRange(range.location+range.length, content.length-location)];
tmpString=[NSMutableString stringWithString:tmp];
range=[tmp rangeOfString:obj];
if (!repeat) {
break;
}
} }];
} return attString; }
如果有什么问题可以向我反馈一下,谢谢
感觉大家阅读。
iOS中NSAttributedString的使用--对关键字着色,以及处理html实例的更多相关文章
- iOS中属性Property的常用关键字的使用说明
属性关键字的作用 现在我们iOS开发中,基本都是使用ARC(自动引用计数)技术,来编写我们的代码.因此在属性property中我们经常使用的关键字有strong,weak,assign,copy,no ...
- iOS中assign、copy 、retain等关键字的含义
iOS中assign.copy .retain等关键字的含义 转自:http://my.oschina.net/majiage/blog/267409 assign: 简单赋值,不更改索引计数cop ...
- iOS中常用属性的关键字的使用说明
属性关键字的作用 现在我们iOS开发中,基本都是使用ARC(自动引用计数)技术,来编写我们的代码.因此在属性property中我们经常使用的关键字有strong,weak,assign,copy,no ...
- iOS 中 const static extern 关键字总结
在看一些高手所写的代码时,总是可以看到我们小白平常不用的关键字,一次,两次,三次,不能总是不明不白,现在总结一下日常开发中常用的关键字的作用: 关键字const/static/extern的释义和用法 ...
- iOS中__block 关键字的底层实现原理
在 <iOS面试题集锦(附答案)> 中有这样一道题目: 在block内如何修改block外部变量?(38题)答案如下: 默认情况下,在block中访问的外部变量是复制过去的,即:写操作不对 ...
- 在iOS中如何正确的实现行间距与行高
最近准备给 VirtualView-iOS 的文本元素新增一个 lineHeight 属性,以便和 VirtualView-Android配合时能更精确的保证双平台的一致性.面向 Google 以及 ...
- iOS中数据库应用基础
iOS 数据库入门 一.数据库简介 1.什么是数据库? 数据库(Database) 是按照数据结构来组织,存储和管理数据的仓库 数据库可以分为2大种类 关系型数据库(主流) PC端 Oracle My ...
- 转iOS中delegate、protocol的关系
iOS中delegate.protocol的关系 分类: iOS Development2014-02-12 10:47 277人阅读 评论(0) 收藏 举报 delegateiosprocotolc ...
- IOS中调用系统的电话、短信、邮件、浏览功能
iOS开发系列--通讯录.蓝牙.内购.GameCenter.iCloud.Passbook系统服务开发汇总 2015-01-13 09:16 by KenshinCui, 26990 阅读, 35 评 ...
随机推荐
- SICP习题练习
练习1.6 new-if的三个参数会先被执行,这样就会无限循环下去 练习1.7 (define (sqrt-iter last-guess guess x) (if (good-enough? las ...
- JAVA线程同步 (二)notify()与notifyAll()-***
编写多线程程序需要进行线程协作,前面介绍的利用互斥来防止线程竞速是来解决线程协作的衍生危害的.编写线程协作程序的关键是解决线程之间的协调问题,在这些任务中,某些可以并行执行,但是某些步骤需要所有的任务 ...
- 杂项-Java:Tomcat
ylbtech-杂项-Java:Tomcat 1.返回顶部 1. Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目, ...
- STM32 的堆栈静态区
STM32的分区从0x2000 0000开始.静态区,堆,栈. 所有的全局变量,包括静态变量之类的,全部存储在静态存储区.紧跟静态存储区之后的,是堆区(如没用到malloc,则没有该区),之后是栈区, ...
- Eclipse中直接执行sql语句(图文说明)
转自:https://blog.csdn.net/changjyzzu/article/details/45487847 1.首先新建sql文件,然后打开文件 22.右键点击空白处,点击set-con ...
- javascript BOM基本知识
1.BOM(Bowser Object Model浏览器对象模型) 浏览器创建的对象通常称作文档(Document)对象,它是浏览器使用的众多对象的一部分,浏览器操作的对象结合起来称作浏览器对象模型( ...
- 0627-TP整理三(对表的操作,数据的显示)
一.对表的操作 直接sql语句:(query/execute) 1.查询: 查询所有:M('表名')->select(); 查询一条数据:M('表名')->find(); 条件查询: 动态 ...
- bzoj 1233: [Usaco2009Open]干草堆tower【dp+单调栈】
参考:https://www.cnblogs.com/N-C-Derek/archive/2012/07/11/usaco_09_open_tower.html 虽然长得很像斜率优化,但是应该不算-- ...
- hdu4738(边双连通分量,桥)
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Linux 搭建Discuz论坛
title: Linux 搭建Discuz论坛 Welcome to Fofade's Blog! 这里是Linux 搭建论坛的一些命令记录 命令摘记: 下载文件:Discuz 安装环境:PHP Ap ...