iOS 图文混排,表情键盘。

注意:新做了一个支持gif的,键盘封装的比较好:新项目github地址

************************************************************************************

类似微信的图(表情)文(文字)混排聊天,以及表情键盘。

给几张实现的效果图:

iOS 图文混排,表情键盘。_第1张图片
iOS 图文混排,表情键盘。_第2张图片
iOS 图文混排,表情键盘。_第3张图片

简单的来说一下流程和实现思路:点击键盘上的表情按钮->将表情转变成一段某种规则(自定义)的字符串(类似[哈哈])->将所有的需要发送消息的字符串(比如:你好呀[哈哈])发送给后台->客户端收到消息通过某种规则转换,把消息字符串转换成文字加表情展示出来。

简单的讲一下转码替换过程:

自定义一个转码工具类:在类里声明并实现一个方法:+ (NSMutableAttributedString*)changeStrWithStr:(NSString*)string Font:(UIFont*)font TextColor:(UIColor*)textColor

讲一下工具类的实现思路(最后会给出demo):

1.第一步加载对照表,并设置富文本参数:

//读取并加载对照表

NSString*path = [[NSBundlemainBundle]pathForResource:@"LiuqsEmoji"ofType:@"plist"];

_emojiImages= [NSDictionarydictionaryWithContentsOfFile:path];

//设置文本参数

NSMutableParagraphStyle*paragraphStyle = [[NSMutableParagraphStylealloc]init];

[paragraphStylesetLineSpacing:4.0f];

NSDictionary*dict = [NSDictionarydictionaryWithObjectsAndKeys:_textColor,NSForegroundColorAttributeName,_font,NSFontAttributeName,paragraphStyle,NSParagraphStyleAttributeName,nil];

CGSizemaxsize =CGSizeMake(1000,MAXFLOAT);

_emotionSize= [@"/"boundingRectWithSize:maxsizeoptions:NSStringDrawingUsesLineFragmentOriginattributes:dictcontext:nil].size;

_attStr= [[NSMutableAttributedStringalloc]initWithString:_stringattributes:dict];

2.正则匹对字符中的表情并保存结果

//比对结果

NSString*regexString =checkStr;

NSRegularExpression*regex = [NSRegularExpressionregularExpressionWithPattern:regexStringoptions:NSRegularExpressionCaseInsensitiveerror:nil];

NSRangetotalRange =NSMakeRange(0, [_stringlength]);

_matches= [regexmatchesInString:_stringoptions:0range:totalRange];


3.遍历结果数组,并将表情编码用图片附件替换,转成富文本并用字典数组保存富文本和对应的range

NSMutableArray*imageDataArray = [NSMutableArrayarray];

//遍历结果

for(inti = (int)_matches.count-1; i >=0; i --) {

NSMutableDictionary*record = [NSMutableDictionarydictionary];

LiuqsTextAttachment*attachMent = [[LiuqsTextAttachmentalloc]init];

attachMent.emojiSize=CGSizeMake(_emotionSize.height,_emotionSize.height);

NSTextCheckingResult*match = [_matchesobjectAtIndex:i];

NSRangematchRange = [matchrange];

NSString*tagString = [_stringsubstringWithRange:matchRange];

NSString*imageName = [_emojiImagesobjectForKey:tagString];

if(imageName ==nil|| imageName.length==0)continue;

//这里有个问题,需要先判断是否有图片,没有图片的话应该不操作,因为现在换用html处理,先不改!

NSString*cheakStr = [imageNamesubstringWithRange:NSMakeRange(1,2)];

if([cheakStrintValue] >60) {

NSString*path = [[NSBundlemainBundle]pathForResource:imageNameofType:@"gif"];

NSData*gifData = [NSDatadataWithContentsOfFile:path];

attachMent.contents= gifData;

attachMent.fileType=@"gif";

}else{

attachMent.image= [UIImageimageNamed:imageName];

}

NSAttributedString*imageStr = [NSAttributedStringattributedStringWithAttachment:attachMent];

[recordsetObject:[NSValuevalueWithRange:matchRange]forKey:@"range"];

[recordsetObject:imageStrforKey:@"image"];

[imageDataArrayaddObject:record];

}

_imageDataArray= imageDataArray;

4.遍历上一步的字典数组,从后往前用表情附件替换表情编码(一定要从后往前),不然range改变回造成错误。

最后返回结果,用label或者textview等加载就OK了。

NSMutableAttributedString*result =_attStr;

for(inti =0; i <_imageDataArray.count; i ++) {

NSRangerange = [_imageDataArray[i][@"range"]rangeValue];

NSDictionary*imageDic = [_imageDataArrayobjectAtIndex:i];

NSMutableAttributedString*imageStr = [imageDicobjectForKey:@"image"];

[resultreplaceCharactersInRange:rangewithAttributedString:imageStr];

}

_resultStr= result;

demo地址


然后是swift版本的地址:swift版本

你可能感兴趣的:(iOS 图文混排,表情键盘。)