一般在我们发微博、或者查看微博内容的时候,常常会有一些表情,但是在我们自己写代码显示的时候,
表情是不会显示出来的,往往是以[哈哈]这种形式存在;那我们就需要把文字转换为表情。
1、导入第三方框架(Regexkitlite)网上搜索就有,
导入之后还要配置 a、将文件设置为arc下运行 -fno-objc-arc
导入系统框架 libicucore.dylib
2、写一个类方法 ,把文本内容传过来进行匹配表情
+ (NSString *)parseFaceText:(NSString *)text;
//通过正则表达式查找表情
NSString *regexFace = @"\\[\\w+\\]";
//查找text中的 表情全部放到数组中
NSArray *faceArray = [text componentsMatchedByRegex:regexFace];
for (NSString *faceName in faceArray) {
//faceName:[呵呵] ---> <image url = '00x.png'>
//获取本地文件的表情plish文件(可去网上获取)
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"emoticons" ofType:@"plist"];
NSArray *faceConfig = [NSArray arrayWithContentsOfFile:filePath];
//通过谓词筛选出符合条件的约束
NSString *t = [NSString stringWithFormat:@"self.chs = '%@'", faceName];
NSPredicate *predicate = [NSPredicate predicateWithFormat:t];
NSArray *items = [faceConfig filteredArrayUsingPredicate:predicate];
if (items.count > 0) {
//有图片对应相应的文字
NSDictionary *faceItem = items[0];
NSString *imageName = [faceItem objectForKey:@"png"];
//构建替换的字符串
NSString *replaceName = [NSString stringWithFormat:@"<image url = '%@'>", imageName];
//替换字符串
text = [text stringByReplacingOccurrencesOfString:faceName withString:replaceName];
}