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