消息类型 | 显示效果图 |
文本类消息 | |
图片类消息 | |
语音类消息 | |
视频类消息 | |
文件类消息 | |
onRecvNewMessage
函数内接收自定义消息。
收到的自定义消息最终会以 Cell
的形式展示在消息列表中,Cell
绘制所需的数据我们称之为 CellData
。TUIChat/UI_Classic/Cell/CellData/Custom
文件夹下新建 TUILinkCellData.h 和 TUILinkCellData.m 文件,继承自TUIMessageCellData
,用于存储显示的文字和跳转的链接。
示例代码如下:@interface TUILinkCellData : TUIMessageCellData@property NSString *text;@property NSString *link;@end
getCellData:
方法。用于把 V2TIMMessage
转换成消息列表 Cell
的绘制数据 TUILinkCellData
。
示例代码如下:@implementation TUILinkCellData(TUIMessageCellData *)getCellData:(V2TIMMessage *)message{NSDictionary *param = [NSJSONSerialization JSONObjectWithData:message.customElem.data options:NSJSONReadingAllowFragments error:nil];TUILinkCellData *cellData = [[TUILinkCellData alloc] initWithDirection:message.isSelf ? MsgDirectionOutgoing : MsgDirectionIncoming];cellData.innerMessage = message;cellData.msgID = message.msgID;cellData.text = param[@"text"];cellData.link = param[@"link"];cellData.avatarUrl = [NSURL URLWithString:message.faceURL];return cellData;}@end
getDisplayString:
方法。用于把 V2TIMMessage
转换成会话列表 lastMsg
的展示文本信息。
会话列表 lastMsg
展示文本指的是当用户停留在会话列表,每个会话 cell 会显示当前会话最后一条消息。如下图所示:@implementation TUILinkCellData+ (NSString *)getDisplayString:(V2TIMMessage *)message {NSDictionary *param = [NSJSONSerialization JSONObjectWithData:message.customElem.data options:NSJSONReadingAllowFragments error:nil];return param[@"text"];}@end
contentSize:
方法,用于计算 cellData
内容所占绘制区域的大小。
示例代码如下:(CGSize)contentSize{CGRect rect = [self.text boundingRectWithSize:CGSizeMake(300, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:15] } context:nil];CGSize size = CGSizeMake(ceilf(rect.size.width)+1, ceilf(rect.size.height));// 加上气泡边距size.height += 60;size.width += 20;return size;}
TUIChat/UI_Classic/Cell/CellUI/Custom
文件夹下新建 TUILinkCell.h 和 TUILinkCell.m 文件,继承自 TUIMessageCell
,用于绘制 TUILinkCellData
数据。
示例代码如下:@interface TUILinkCell : TUIMessageCell@property UILabel *myTextLabel; // 展示文本@property UILabel *myLinkLabel; // 链接跳转文本(void)fillWithData:(TUILinkCellData *)data; // 绘制 UI@end
initWithStyle:reuseIdentifier:
方法,创建 myTextLabel
和 myLinkLabel
文本展示对象,并添加至 container
容器。
示例代码如下:@implementation TUILinkCell// 初始化控件(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];if (self) {self.myTextLabel = [[UILabel alloc] init];[self.container addSubview:self.myTextLabel];self.myLinkLabel = [[UILabel alloc] init];self.myLinkLabel.text = @"查看详情>>";[self.container addSubview:_myLinkLabel];}return self;}@end
fillWithData:
方法,在 TUILinkCell
中自定义展示 TUILinkCellData
数据。
示例代码如下:@implementation TUILinkCell// 根据 cellData 绘制 cell(void)fillWithData:(TUILinkCellData *)data;{[super fillWithData:data];self.myTextLabel.text = data.text;}@end
layoutSubviews
方法,自定义控件的布局。
示例代码如下:// 设置控件坐标(void)layoutSubviews{[super layoutSubviews];self.myTextLabel.mm_top(10).mm_left(10).mm_flexToRight(10).mm_flexToBottom(50);self.myLinkLabel.mm_sizeToFit().mm_left(10).mm_bottom(10);}@end
cell
和 cellData
创建完成后,需要您在 TUIMessageDataProvider.m 的 load
函数里主动注册 cell
和 cellData
信息。
注册完成后,消息列表在收到消息时会根据 businessID
自动找到对应的 cellData
处理消息数据,消息列表在刷新 UI 的时候,也会根据 businessID
自动创建对应 Cell
绘制 cellData
数据。@implementation TUIMessageDataProvider+ (void)load {// 以下代码需要您自己实现customMessageInfo = @[@{@"businessID" : @"custom_message_link", // 自定义消息唯一标识(注意不要重复)@"cell_name" : @"TUILinkCell" // cell 的类名@"cell_data_name" : @"TUILinkCellData" // cellData 的类名},// 如果您需要多种类型的自定义消息,可以在下面继续添加自定义消息信息@{@"businessID" : @"custom_message_link2",@"cell_name" : @"TUILinkCell2"@"cell_data_name" : @"TUILinkCellData2"}];}@end
@implementation TUIChatDataProvider+ (void)load {// 以下代码需要您自己实现customButtonInfo = @[@{@"SendBtn_Key" : @"custom_link_btn", // 按钮唯一标识@"SendBtn_Title" : @"自定义" // 按钮文本信息@"SendBtn_ImageName" : @"custom_link_image" // 按钮图片名称}];}@end
didSelectMoreCell
回调通过 createCustomMessage 接口创建一条自定义消息,其中参数 data
可以由 json
数据组成,可以在 json
数据里面自定义一个 businessID
字段来唯一标识这条消息。
示例代码如下:@implementation TUIMessageController- (void)inputController:(TUIInputController *)inputController didSelectMoreCell:(TUIInputMoreCell *)cell{if ([cell.data.key isEqualToString:@"custom_link_btn"]) {// 创建自定义消息,设置消息的 businessID、展示文本、跳转链接(以下代码需要您自己实现)NSString *businessID = @"custom_message_link";NSString *text = @"欢迎加入腾讯·云通信大家庭!";NSString *link = @"https://www.tencentcloud.com/document/product/269/3794?from_cn_redirect=1";NSDictionary *param = @{@"businessID": businessID, @"text":text, @"link":link};NSData *data = [NSJSONSerialization dataWithJSONObject:param options:0 error:&error];V2TIMMessage *message = [[V2TIMManager sharedInstance] createCustomMessage:data];[self sendMessage:message];}}@end
本页内容是否解决了您的问题?