消息类型 | 显示效果图 |
文本类消息 | |
图片类消息 | |
语音类消息 | |
视频类消息 | |
文件类消息 | |
onRecvNewMessage
函数内接收自定义消息。
收到的自定义消息最终会以 Cell
的形式展示在消息列表中,Cell
绘制所需的数据我们称之为 CellData
。TUIChat/BaseCellData/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
TUIChat/UI_Minimalist/Cell/Custom
文件夹下新建 TUILinkCell_Minimalist.h 和 TUILinkCell_Minimalist.m 文件,继承自 TUIBubbleMessageCell_Minimalist
,用于绘制 TUILinkCellData
数据。
示例代码如下:@interface TUILinkCell_Minimalist :TUIBubbleMessageCell_Minimalist
@property UILabel *myTextLabel; // Display text@property UILabel *myLinkLabel; // Link redirection text- (void)fillWithData:(TUILinkCellData *)data;// Draw UI@end
initWithStyle:reuseIdentifier:
方法,创建 myTextLabel
和 myLinkLabel
文本展示对象,并添加至 container
容器。
示例代码如下:@implementation TUILinkCell_Minimalist// Initialize the control- (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 = @"View details>>";[self.container addSubview:_myLinkLabel];}return self;}@end
fillWithData:
方法,在 TUILinkCell_Minimalist
中自定义展示 TUILinkCellData
数据。
示例代码如下:@implementation TUILinkCell_Minimalist// Draw the cell based on cellData- (void)fillWithData:(TUILinkCellData *)data;{[super fillWithData:data];self.myTextLabel.text = data.text;}@end
layoutSubviews
方法,自定义控件的布局。
示例代码如下:// Set the control coordinates- (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
+ (CGSize)getContentSize:(TUIMessageCellData *)data {NSAssert([data isKindOfClass:TUILinkCellData.class], @"data must be kind of TUILinkCellData");TUILinkCellData *linkCellData = (TUILinkCellData *)data;CGFloat textMaxWidth = 245.f;CGRect rect = [linkCellData.text boundingRectWithSize:CGSizeMake(textMaxWidth, MAXFLOAT)options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeadingattributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]}context:nil];CGSize size = CGSizeMake(textMaxWidth + 15, rect.size.height + 56);return size;}
@implementation TUIMessageCellConfig_Minimalist (CustomMessageRegister)+ (void)registerExternalCustomMessageInfo {// Insert your own custom message UI here, your businessID can not be same with built-in //// Example:[self registerCustomMessageCell:@"TUILinkCell_Minimalist" messageCellData:@"TUILinkCellData" forBusinessID:BussinessID_TextLink];}
// The custom message's businessID (note that there should not be a duplication)#define BussinessID_TextLink @"text_link"/** Register custom message's to TUIChat. The three parameters are* @param businessID Custom message's businessID* @param messagellClass Custom message's NSString type* @param messageCellDataClassName Custom message's NSString type* @param styleType UI style corresponding to this custom message, for example TUIChatRegisterCustomMessageStyleTypeMinimalist*/- (void)registerCustomMessageCell {[TUIChatConfig.defaultConfig registerCustomMessage:BussinessID_TextLinkmessageCellClassName:@"TUILinkCell_Minimalist"messageCellDataClassName:@"TUILinkCellData"styleType:TUIChatRegisterCustomMessageStyleTypeMinimalist];}
title
和图片leftMark
组成。您可以通过在 TUIChatDataProvider 的 customInputMoreActionItemList
属性中新增 TUICustomActionSheetItem
对象来添加自定义按钮。TUICustomActionSheetItem
的title
和leftMark
属性来自定义您想展示的文字和图片信息;如果您想调整按钮的展示顺序,可以设置 priority
属性,其中priority
值越大按钮越靠前;您也可以设置actionHandler
来响应该按钮的点击事件,实现自己的业务逻辑。@implementation TUIChatDataProvider- (NSArray<TUICustomActionSheetItem *> *)customInputMoreActionItemList {if (_customInputMoreActionItemList == nil) {NSMutableArray *arrayM = [NSMutableArray array];if (TUIChatConfig.defaultConfig.enableWelcomeCustomMessage) {__weak typeof(self) weakSelf = self;TUICustomActionSheetItem *link =[[TUICustomActionSheetItem alloc] initWithTitle:TIMCommonLocalizableString(TUIKitMoreLink)leftMark:[UIImage imageNamed:TUIChatImagePath_Minimalist(@"icon_more_custom")]withActionHandler:^(UIAlertAction *_Nonnull action) {link.priority = 100;NSString *text = TIMCommonLocalizableString(TUIKitWelcome);NSString *link = TUITencentCloudHomePageEN;NSString *language = [TUIGlobalization tk_localizableLanguageKey];if ([language containsString:@"zh-"]) {link = TUITencentCloudHomePageCN;}NSError *error = nil;NSDictionary *param = @{BussinessID : BussinessID_TextLink, @"text" : text, @"link" : link};NSData *data = [NSJSONSerialization dataWithJSONObject:param options:0 error:&error];if (error) {NSLog(@"[%@] Post Json Error", [self class]);return;}V2TIMMessage *message = [TUIMessageDataProvider getCustomMessageWithJsonData:data desc:text extension:text];if ([weakSelf.delegate respondsToSelector:@selector(dataProvider:sendMessage:)]) {[weakSelf.delegate dataProvider:weakSelf sendMessage:message];}}];[arrayM addObject:link];}_customInputMoreActionItemList = [NSArray arrayWithArray:arrayM];}return _customInputMoreActionItemList;}@end
本页内容是否解决了您的问题?