/*
1.创建NSTextAttachment对象
2.设置image属性为需要展示的图片
3.设置bounds属性为需要展示的大小
4.通过NSTextAttachment对象生成NSAttributedString对象
5.通过字符串创建NSMutableAttributedString
6.把NSAttributedString插入到NSMutableAttributedString的任意位置
7.把NSMutableAttributedString赋值给attributedText
*/
//在文本中加入表情
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 300, 200)];
label.numberOfLines= 0;
NSString *text =@"这是一个表情";
UIFont *font = [UIFont systemFontOfSize:30];
label.font = font;
label.text = text;
[self.view addSubview:label];
//创建NSTextAttachment的对象文本附件
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
//创建图片
UIImage *image = [UIImage imageNamed:@"8.jpg"];
//在文本附件上设置图片
attachment.image= image;
//设置图片大小图片宽高和文字宽高一致
attachment.bounds=CGRectMake(0, 0, font.lineHeight, font.lineHeight);
//根据attachment创建属性字符串
NSAttributedString *attr = [NSAttributedString attributedStringWithAttachment:attachment];
//创建可变属性字符串将原本的text的内容转化为可变的字符串
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:text];
//选择插入的文字上的位置第6位
[attrStr insertAttributedString:attrat Index:6];
//替换
[attrStr replaceCharactersInRange:NSMakeRange(5, 1) withAttributedString:attr];
//添加labe进行显示
label.attributedText = attrStr;
//通过资源路径获取图片进行显示
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 300, 200)];
label.numberOfLines= 0;
UIFont *font = [UIFont systemFontOfSize:30];
label.font= font;
[self.view addSubview:label];
NSString *text =@"今天吃饭吃了三碗饭,好开心[笑哈哈],但是胖了三斤[泪流满面]";
//正则表达式
NSString *pattern =@"\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+]";
//创建正则对象
NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
//接收根据正则表达式搜索到的结果数组
NSArray *resultArray = [regular matchesInString:text options:0 range:NSMakeRange(0, text.length)];
//创建可变属性字符串将原本的text的内容转化为可变的字符串
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:text];
//声明偏移量属性
__block NSIntegeroffset = 0;
//遍历结果数组
[resultArray enumerateObjectsUsingBlock:^(NSTextCheckingResult *_Nonnullobj,NSUIntegeridx,BOOL *_Nonnullstop) {
//根据偏移量获取实际是range
NSRange range =NSMakeRange(obj.range.location- offset, obj.range.length);
//取出表情对应的字符串
NSString *face = [text substringWithRange:obj.range];
//创建NSTextAttachment的对象文本附件
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
//根据名字字符串去资源路径中拿取对应的图片
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[self pathForEmoticonWithName:face]];
//把图片添加到文本附件上
attachment.image = image;
//可以不用设置大小,会自动适应
//attachment.bounds = CGRectMake(0, 0, font.lineHeight, font.lineHeight);
//设置文本附件的属性字符串
NSAttributedString *att = [NSAttributedString attributedStringWithAttachment:attachment];
//图片代替文字
[attrStr replaceCharactersInRange:range withAttributedString:att];
//更新偏移量(这样计算下一个的obj.range.location才不会出错)
//obj.range.length -------5-----6
//offset = obj.range.length - 1 -----4---5
//offset = offset + offset--------4----9
offset += obj.range.length-1;
}];
//加载到label上
label.attributedText= attrStr;
}
//根据图片名字获取图片资源的具体路径
- (NSString *)pathForEmoticonWithName:(NSString *)name {
//资源在哪
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"Emoticons" ofType:@"bundle"];
//拼接资源路径
NSString *path1 = [bundlePath stringByAppendingPathComponent:@"com.sina.lxh/info.plist"];
//接收从资源路径获取的字典内容
_dic = [NSDictionary dictionaryWithContentsOfFile:path1];
//从字典中提取出图片包
NSArray *array =_dic[@"emoticons"];
//开始设置要返回的图片资源路径
NSString *path =nil;
//遍历表情包数组里的字典
for(NSDictionary *dicinarray) {
//从字典里找出名字对应的元素
if([dic[@"chs"] isEqualToString:name]) {
//找出图片名字
NSString *png = [dic objectForKey:@"png"];
//拼接出具体的图片资源路径 png = "lxh_xiaohaha.png";
path = [bundlePath stringByAppendingPathComponent:[NSStringstringWithFormat:@"com.sina.lxh/%@",png]];
break;
}
}
//返回具体路径
return path;
}