iOS聊天表情的实现
//内容处理的方法,继承于UITextView的类(即self为UITextView实例对象), 返回一个处理好的UITextView(包括链接,电话,表情排列等)
//表情处理方法一
NSDictionary *attributes =@{NSFontAttributeName:[UIFont systemFontOfSize:textFont]};//textFont文字大小
NSMutableAttributedString *mutableAttribute = [[NSMutableAttributedString alloc]initWithString:contentText attributes:attributes];//contentText聊天内容
//表情(_imagesRegexString为表情正则)
if(_imagesRegexString) {
NSRegularExpression *emotionExpression = [NSRegularExpression regularExpressionWithPattern:_imagesRegexString options:NSRegularExpressionCaseInsensitive error:nil];
NSArray*matchs = [emotionExpression matchesInString:contentText options:0range:NSMakeRange(0, contentText.length)];
intrangeDelete =0;//删除一个表情字符后range的location要前移
for(NSTextCheckingResult *result in matchs) {
NSRange range = [result range];
NSString*currentEmojiString = [contentText substringWithRange:range];//表情显示字符
NSString*emotionImageName = [self loadEmotionImage:currentEmojiString];//表情图片名称
UIImage*image = [UIImage imageNamed:emotionImageName];
if(emotionImageName.length!=0&& image) {
NSTextAttachment*attachment = [[NSTextAttachment alloc]init];
attachment.image= [UIImage imageNamed:emotionImageName];
attachment.bounds=CGRectMake(0, -5, emotionSize.width, emotionSize.height);
[mutableAttribute deleteCharactersInRange:NSMakeRange(range.location- rangeDelete, range.length)];
NSAttributedString*attributeString = [NSAttributedString attributedStringWithAttachment:attachment];
[mutableAttribute insertAttributedString:attributeStringatIndex:range.location- rangeDelete];
rangeDelete += range.length-1;//前移删除表情字符长度的累加(插入的表情图片占1个长度)
//表情处理方法二
NSMutableString *emojiString = [NSMutableString stringWithString:contentText];
NSMutableAttributedString *mutableAttribute = [[NSMutableAttributedString alloc] init];
NSMutableArray *attributes = [NSMutableArray array];
//根据正则一次获取符合的字符结果
NSTextCheckingResult *result = [expression firstMatchInString:emojiString options:NSMatchingReportProgress range:NSMakeRange(0, emojiString.length)];
while (result) {
NSRange range = [result rangeAtIndex:0];
NSString *prefixNotEmojiString = [emojiString substringToIndex:range.location];
NSString *currentEmojiString = [emojiString substringWithRange:range];
if (prefixNotEmojiString.length) {
NSAttributedString *attributeString = [[NSAttributedString alloc] initWithString:prefixNotEmojiString attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:textFont]}];
[attributes addObject:attributeString];
}
if (currentEmojiString.length) {
NSString *emotionImageName = [self loadEmotionImage:currentEmojiString];
UIImage *image = [UIImage imageNamed:emotionImageName];
//表情名称字符不存在或表情图片不存在,直接显示字符而不是表情
if (emotionImageName.length == 0 || !image) {
NSAttributedString *attributeString = [[NSAttributedString alloc] initWithString:currentEmojiString attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:textFont]}];
[attributes addObject:attributeString];
}else{
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:emotionImageName];
attachment.bounds = CGRectMake(0, -5, emotionSize.width, emotionSize.height);
NSAttributedString *attributeString = [NSAttributedString attributedStringWithAttachment:attachment];
[attributes addObject:attributeString];
}
}
//删除已经处理的,更新正则结果
[emojiString deleteCharactersInRange:NSMakeRange(0, range.location+range.length)];
result = [expression firstMatchInString:emojiString options:NSMatchingReportProgress range:NSMakeRange(0, emojiString.length)];
}
//最后添加上后面的非表情字符
NSAttributedString *attributeString = [[NSAttributedString alloc]initWithString:emojiString];
[attributes addObject:attributeString];
[attributes enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[mutableAttribute appendAttributedString:obj];
}];
//@功能(_singleRegexString @正则)
if(_singleRegexString) {
NSRegularExpression*singleExpression = [NSRegularExpression regularExpressionWithPattern:_singleRegexString options:NSRegularExpressionCaseInsensitive error:nil];
NSArray*singlematchs = [singleExpression matchesInString:[mutableAttribute string]options:0range:NSMakeRange(0, mutableAttribute.length)];
if(singlematchs.count>0) {
NSRange range = [singlematchs[0] range];
if(range.location==0) {//@功能只能在第一个字符
NSString*singleString = [[mutableAttribute string] substringWithRange:range];
if(singleString.length<=24) {//@的人名字限定在24个字符
[mutableAttribute addAttribute:NSForegroundColorAttributeName value:_singleColor range:range];
[mutableAttribute addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleNone) range:range];
}
}
}
}
//行间距(_lineSpace默认值为0)
if(_lineSpace) {
NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc]init];
[paragraphStyle setLineSpacing:_lineSpace];
[mutableAttribute addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [mutableAttribute length])];
}
//设置连接textView自带检索连接,电话,地址等功能,无需手动设置
self.delegate=self;
self.dataDetectorTypes=UIDataDetectorTypeLink|UIDataDetectorTypePhoneNumber;
self.editable=NO;//非编辑状态下才可以点击Url
self.scrollEnabled=NO;
self.attributedText= mutableAttribute;
CGSizesize = [self sizeThatFits:CGSizeMake(contentWidth,CGFLOAT_MAX)];
self.size= size;
//UITextView代理,匹配电话/链接等
- (BOOL)textView:(UITextView*)textView shouldInteractWithURL:(NSURL*)URL inRange:(NSRange)characterRange {
NSLog(@"url :%@",URL);
//点击电话号码
if([[URLscheme]isEqualToString:@"tel"]) {
NSString*telephone = [URLresourceSpecifier];
NSLog(@"电话号码为:%@",telephone);
return NO;
}
return YES;
}