项目介绍:
LiuqsChatWithEmotion https://github.com/LMMIsGood/LiuqsChatWithEmotion
首先声明,这里只是提供一个方案,是我在自己的项目中使用的代码:
1.自定义表情键盘(LiuqsEmotionView)
2.自定义表情编码(EmotionCodeTable,自己项目中的已经改成plist加载,因为每个项目都有差异性,你可以自己写编码,以及编码对照的图片的规则,demo中参考一下就行)
3.编码转换工具类(LiuqsChangeStrTool 用来展示编码字符串)
由于项目的差异性,无法针对性封装,集成起来并不简单,所以我会介绍一下每个类的功能以及如何使用,你完全可以参照思路自定义整套东西。
1.表情键盘 LiuqsEmotionView
ios <wbr>图文混排 <wbr>表情键盘!
图上这一部分为键盘,并不包括输入框,和toolbar上的按钮(因为不同项目的差异性)!
使用代码:
//懒加载创建表情键盘
[Objective-C] 查看源文件 复制代码
-(LiuqsEmotionView *)emotionview
{
if (!_emotionview) {
_emotionview = [[LiuqsEmotionView alloc]initWithFrame:emotionDownFrame];
self.emotionview.IputView = self.toolBarView.textView;
self.emotionview.delegate = self;
[self.view addSubview:self.emotionview];
}
return _emotionview;
弹出键盘的代码:
[Objective-C] 查看源文件 复制代码
#pragma mark - ToolBar代理方法
-(void)ToolbarEmotionBtnDidClicked:(UIButton *)emotionBtn
{
if (emotionBtn.selected) {//判断辨清键盘的弹收状态,已经弹起,就执行收起事件
emotionBtn.selected = NO;
[self.textView becomeFirstResponder];
self.tableView.height = screenH - self.keyBoardH - self.toolBarView.height - 64;
}else
{//为弹起状态就弹起emotionUpFrame 为表情键盘弹起时的frame;
[self.textView resignFirstResponder];
emotionBtn.selected = YES;
[UIView animateWithDuration:emotionTipTime animations:^{
self.emotionview.frame = emotionUpFrame;
self.toolBarView.frame = CGRectMake(0, screenH - self.toolBarView.height - self.emotionview.height, screenW, self.toolBarView.height);
self.tableView.height = screenH - self.emotionview.height - self.toolBarView.height - 64;
if (self.tableView.contentSize.height > self.tableView.height) {
[self.tableView setContentOffset:CGPointMake(0, self.tableView.contentSize.height - self.tableView.bounds.size.height + 3) animated:NO];
}
}];
}
}
}
这是自定义toobar的代理方法,其实就是点击了toolbar上的表情按钮是要实行的方法,也就是表情键盘弹起的方法.
2. 转码工具LiuqsChangeStrTool
输入已经解决,那么输出也就是展示肯定也要有,不能发出去是编码,收到展示的还是编码吧!这里介绍一下如何使用这个类:
[Objective-C] 查看源文件 复制代码
//匹对字符串,获取富文本
NSMutableAttributedString *text = [LiuqsChangeStrTool changeStrWithStr:message.text andFont:[UIFont systemFontOfSize:17.0f]];
CGSize maxsize = CGSizeMake(screenW - 20, MAXFLOAT);
//设置富文本的参数
[text addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17.0f] range:NSMakeRange(0, text.length)];
//文字自适应
CGSize TextSize = [text boundingRectWithSize:maxsize options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
self.message.attributedText = (NSAttributedString*)text;
//计算控件frame
self.emotionLabelFrame = CGRectMake(10, 10, TextSize.width, TextSize.height);
//计算cell高度
self.cellHeight = TextSize.height + 20;
在计算frame的类中将NSString类型的编码(也就是拿来通讯的字符串)通过转换工具转换成NSAttributedString,最后在cell中赋值给label展示就ok了!
内容比较多,能简单说的就这些,介绍的笼统多多包涵,大家可以下载demo查看详细的实现,如果有什么问题可以留言或者发邮件给我:877520617@qq.com。
DEMO 下载:
