04环信聊天界面 - 完善语音消息在cell上的显示
来源:互联网 发布:jq源码整体思路 编辑:程序博客网 时间:2024/05/17 03:43
1.在发送语言的方法里,把语言消息添加到数据源,并刷新表格
/** * 发送语音 * * @param recordPath 语音文件路径 * @param duration 时间 */- (void)sendVoice:(NSString *)recordPath duration:(NSInteger)duration{ // 1.构造一个 语音消息体 EMChatVoice *chatVoice = [[EMChatVoice alloc] initWithFile:recordPath displayName:@"[语音]"]; EMVoiceMessageBody *voiceBody = [[EMVoiceMessageBody alloc] initWithChatObject:chatVoice]; voiceBody.duration = duration; // 2.构造一个消息对象 EMMessage *msg = [[EMMessage alloc] initWithReceiver:self.buddy.username bodies:@[voiceBody]]; msg.messageType = eMessageTypeChat; // 3.发送 [[EaseMob sharedInstance].chatManager asyncSendMessage:msg progress:nil prepare:^(EMMessage *message, EMError *error) { //准备发送语音 } onQueue:nil completion:^(EMMessage *message, EMError *error) { if (!error) { //语音发送成功 }else{ //语音发送失败 } } onQueue:nil]; // 4.把消息添加到数据源,然后刷新表格 [self.dataSources addObject:msg]; [self.tableView reloadData]; // 5.滚动tableView [self scrollToBottom];}
2.让cell展示语言消息的样式
//// ChatCell.m#import "ChatCell.h"#import "EaseMob.h"@implementation ChatCell- (CGFloat)cellHeight{ // 1.重新布局子控件(后label的高度已经确定了) [self layoutIfNeeded]; // 2.返回cell的高度 return self.messageLabel.frame.size.height + 50;}- (void)setMessage:(EMMessage *)message{ _message = message; // 1.获取消息体 id body = message.messageBodies[0]; if ([body isKindOfClass:[EMTextMessageBody class]]) { //文本消息 EMTextMessageBody *textBody = body; self.messageLabel.text = textBody.text; }else if ([body isKindOfClass:[EMVoiceMessageBody class]]){ //语言消息 self.messageLabel.attributedText = [self voiceAttr]; }else{ self.messageLabel.text = @"未知类型"; }}/** * 返回语音富文本 */- (NSAttributedString *)voiceAttr{ // 创建一个可变的富文本 NSMutableAttributedString *voiceAttM = [[NSMutableAttributedString alloc] init]; if ([self.reuseIdentifier isEqualToString:recivierCell]) { //接收方 // 语言图片 UIImage *receiverImg = [UIImage imageNamed:@"chat_receiver_audio_playing_full"]; // 图片附件 NSTextAttachment *imgAttachment = [[NSTextAttachment alloc] init]; imgAttachment.image = receiverImg; imgAttachment.bounds = CGRectMake(0, -4, 20, 20); // 图片富文本 NSAttributedString *imgAtt = [NSAttributedString attributedStringWithAttachment:imgAttachment]; [voiceAttM appendAttributedString:imgAtt]; // 时间 EMVoiceMessageBody *voiceBody = self.message.messageBodies[0]; NSInteger duration = voiceBody.duration; NSString *timeStr = [NSString stringWithFormat:@"%ld",duration]; NSAttributedString *timeAtt = [[NSAttributedString alloc] initWithString:timeStr]; [voiceAttM appendAttributedString:timeAtt]; }else{ //发送方 // 时间 EMVoiceMessageBody *voiceBody = self.message.messageBodies[0]; NSInteger duration = voiceBody.duration; NSString *timeStr = [NSString stringWithFormat:@"%ld",duration]; NSAttributedString *timeAtt = [[NSAttributedString alloc] initWithString:timeStr]; [voiceAttM appendAttributedString:timeAtt]; // 语言图片 UIImage *receiverImg = [UIImage imageNamed:@"chat_sender_audio_playing_full"]; NSTextAttachment *imgAttachment = [[NSTextAttachment alloc] init]; imgAttachment.image = receiverImg; imgAttachment.bounds = CGRectMake(0, -4 , 20, 20); NSAttributedString *imgAtt = [NSAttributedString attributedStringWithAttachment:imgAttachment]; [voiceAttM appendAttributedString:imgAtt]; } return [voiceAttM copy];}@end
0 0
- 04环信聊天界面 - 完善语音消息在cell上的显示
- 04环信聊天界面 - 显示时间的cell
- 04环信聊天界面 - 播放语音消息
- 04环信聊天界面 - 接收方cell的布局
- 04环信聊天界面 - 计算cell的高度
- 04环信聊天界面 - 发送语音
- 04环信聊天界面 - 完善聊天输入框
- 04环信聊天界面 - 发送聊天消息
- 04环信聊天界面 - 发送图片消息和显示图片
- 04环信聊天界面 - 监听消息回复
- 04环信聊天界面 - 显示历史会话记录
- 聊天界面cell的搭建
- android如何集成环信的聊天界面和消息列表
- swift QQ聊天界面 cell 的计算
- iOS聊天界面cell从下至上滚动
- 环信语音消息听筒和扬声器的切换
- 环信聊天界面的修改(基于官方easeui库)
- swift uitableview 中实现分组,将cell分组显示,类似微信的好友界面
- spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件)
- Android开发中一些被冷落但却很有用的类和方法
- SAP接口编程-RFC系列12 : Table Parameter作为输出参数
- IOS开发中关于数据的本地化存储
- Coredump故障分析
- 04环信聊天界面 - 完善语音消息在cell上的显示
- 11.Oracle深度学习笔记——操作系统自动化监控脚本
- 源代码管理器git
- python列表删除会出现一个错误 list index out of range
- 排序算法(四)——快速排序
- 总结学习Fckeditor使用方法
- C++基础篇之 复制构造函数调用时机
- tableView中实现headView图片拉伸效果的三种解决方案
- 我的C#学习日记:2016.2.19