其實自定義環信的聊天氣泡並沒有想象中那么麻煩,歸根到底只需要三步就能完成,記錄一下自己的一個思路。
1.寫一個BubbleView的類,可參考環信自帶的類,只要是做一些約束的工作,如下。
2.為BubbleView分配一個Identifier,也可參考環信自帶的Identifier的寫法,如下
extern NSString *const EaseMessageCellIdentifierSendText; extern NSString *const EaseMessageCellIdentifierSendLocation; extern NSString *const EaseMessageCellIdentifierSendVoice; extern NSString *const EaseMessageCellIdentifierSendVideo; extern NSString *const EaseMessageCellIdentifierSendImage; extern NSString *const EaseMessageCellIdentifierSendFile; extern NSString *const EaseMessageCellIdentifierSendImageText; extern NSString *const EaseMessageCellIdentifierRecvText; extern NSString *const EaseMessageCellIdentifierRecvLocation; extern NSString *const EaseMessageCellIdentifierRecvVoice; extern NSString *const EaseMessageCellIdentifierRecvVideo; extern NSString *const EaseMessageCellIdentifierRecvImage; extern NSString *const EaseMessageCellIdentifierRecvFile; extern NSString *const EaseMessageCellIdentifierRecvImageText;
3.在bodyType的類型里做判斷顯示,如下
case EMMessageBodyTypeText: { if (model.message.ext == nil) { [_bubbleView setupTextBubbleView]; _bubbleView.textLabel.font = _messageTextFont; _bubbleView.textLabel.textColor = _messageTextColor; } else { [_bubbleView setupImageTextBubbleView]; _bubbleView.fileNameLabel.font = _messageFileNameFont; _bubbleView.fileNameLabel.textColor = _messageFileNameColor; _bubbleView.fileSizeLabel.font = _messageFileSizeFont; } } break;
好多時候,環信已經做好很多工作,我們只需要跟着它的步驟一步一步做下去就行了。