項(xiàng)目中的聊天功能需要發(fā)送和接收?qǐng)D片表情,查閱資料后決定使用富文本實(shí)現(xiàn)似芝。思路如下:
客戶端之間約定一個(gè)標(biāo)識(shí)符并定義一個(gè)正則表達(dá)式蛹磺;
-
將圖片資源的名稱與之保持一致锹安,同時(shí)將資源文件導(dǎo)入到工程里畅买,并新建一個(gè)plist文件便于取值并闲;
收到文本消息時(shí)(本質(zhì)上是一個(gè)文本消息),使用之前約定的正則表達(dá)式對(duì)消息內(nèi)容進(jìn)行匹配并篩選出滿足條件的字符串谷羞;
使用富文本將字符串替換為圖片并插入帝火;
發(fā)送圖片表情消息時(shí),將圖片表情進(jìn)行轉(zhuǎn)義湃缎,轉(zhuǎn)換成字符串犀填。
話不多說(shuō),上代碼
@interface RPConvertToCommonEmoticonsHelper : NSObject
//使用單例進(jìn)行初始化
+ (instancetype)shareInstancetype;
/*!
@method
@brief 表情編碼轉(zhuǎn)換為圖片png表情
@param text 待轉(zhuǎn)換的文字
@return 轉(zhuǎn)換后的富文本
*/
- (NSAttributedString *)convertToPNGEmoticons:(NSString *)text;
/*!
@method
@brief 判斷消息是否包含圖片表情
@param text 待轉(zhuǎn)換的文字
@return 是否包含
*/
- (BOOL)isPNGEmotion:(NSString *)text;
/*!
@method
@brief 圖片png表情轉(zhuǎn)換成字符串(發(fā)送消息的時(shí)候需要轉(zhuǎn)換)
@param attributedString 待轉(zhuǎn)換的富文本
@return 轉(zhuǎn)換后的字符串
*/
- (NSMutableString *)convertAttributedText:(NSAttributedString *)attributedString;
@end
//自定義的文本附件
@interface RPAttachment : NSTextAttachment
@property (nonatomic , copy) NSString * text;
@end
#import "RPConvertToCommonEmoticonsHelper.h"
static RPConvertToCommonEmoticonsHelper * convertToCommonEmoticonsHelper = nil;
@implementation RPConvertToCommonEmoticonsHelper
+ (instancetype)shareInstancetype{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
convertToCommonEmoticonsHelper = [[self alloc] init];
});
return convertToCommonEmoticonsHelper;
}
- (NSAttributedString *)convertToPNGEmoticons:(NSString *)text{
if ([self isPNGEmotion:text]) {
RPLog(@"消息包含圖片表情 == %@ ",text);
//表情標(biāo)識(shí)的正則
NSString *regex = @"你的正則";
NSError *error;
NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:regex options:NSRegularExpressionCaseInsensitive error:&error];
NSMutableAttributedString *string = [[ NSMutableAttributedString alloc ] initWithString:text attributes:nil ];
if (!error) {
// 對(duì)str字符串進(jìn)行匹配
NSArray *matches = [regular matchesInString:text options:0 range:NSMakeRange(0, text.length)];
// 遍歷匹配后的每一條記錄
for (NSTextCheckingResult *match in [matches reverseObjectEnumerator]) {
NSRange range = [match range];
NSString *mStr = [text substringWithRange:range];
RPLog(@"%@", mStr);
//自定義的文本附件 繼承NSTextAttachment 并添加一個(gè)String類(lèi)型的屬性 將來(lái)轉(zhuǎn)義的時(shí)候要用到
RPAttachment *rpAttchImage = [[RPAttachment alloc] init];
rpAttchImage.text = mStr;
// 表情圖片
rpAttchImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",mStr]];
rpAttchImage.bounds = CGRectMake(x, y, width,height);
NSAttributedString *tempString = [NSAttributedString attributedStringWithAttachment:rpAttchImage];
if (rpAttchImage != nil) {
//插入表情
[string deleteCharactersInRange:range];
[string insertAttributedString:tempString atIndex:range.location];
}
}
}
//設(shè)置字體
[string addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, string.length)];
return string;
}else{
NSMutableAttributedString *string = [[ NSMutableAttributedString alloc ] initWithString:text attributes:nil ];
//設(shè)置字體
[string addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, string.length)];
return string;
}
}
- (BOOL)isPNGEmotion:(NSString *)text{
//根據(jù)表情標(biāo)識(shí) 匹配表情
NSRegularExpression *numberRegular = [NSRegularExpression regularExpressionWithPattern:@"你的正則" options:NSRegularExpressionCaseInsensitive error:nil];
NSInteger count = [numberRegular numberOfMatchesInString:text options:NSMatchingReportProgress range:NSMakeRange(0, text.length)];
if (count > 0) {
return YES;
}else{
return NO;
}
}
- (NSMutableString *)convertAttributedText:(NSAttributedString *)attributedString{
//轉(zhuǎn)義回來(lái)
NSMutableString *attStr = [[NSMutableString alloc] initWithString:attributedString.string];
[attributedString enumerateAttribute:NSAttachmentAttributeName
inRange:NSMakeRange(0, attributedString.length)
options:NSAttributedStringEnumerationReverse
usingBlock:^(id value, NSRange range, BOOL *stop){
if (value) {
//拿到文本附件
RPAttachment *attachment = (RPAttachment*)value;
NSString *str = [NSString stringWithFormat:@"%@",attachment.text];
//替換成圖片表情的標(biāo)識(shí)
[attStr replaceCharactersInRange:range withString:str];
}
}];
return attStr;
}
@end
@implementation RPAttachment
@end
效果如下