Message Type | Renderings |
Text message | |
Image message | |
Audio message | |
Video message | |
File message | |
onRecvNewMessage
function in TUIMessageBaseDataProvider.m,
and the received custom message will be displayed in Cell
mode in the message list. The data required for Cell
drawing is called CellData
.TUIChat/UI_Classic/Cell/CellData/Custom
. Inherit data from TUIMessageCellData
to CellData
to store the text to display and the link to redirect.
Sample code:@interface TUILinkCellData : TUIMessageCellData@property NSString *text;@property NSString *link;@end
getCellData:
method of the parent class to convert V2TIMMessage
to the drawing data TUILinkCellData
of the message list Cell
.
Sample code:@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:
method of the parent class to convert V2TIMMessage
to the lastMsg
display text information of the conversation list.
The lastMsg
display text of the conversation list indicates that the last message of the current conversation will be displayed for each conversation Cell. See the figure below:@implementation TUILinkCellData+ (NSString *)getDisplayString:(V2TIMMessage *)message {NSDictionary *param = [NSJSONSerialization JSONObjectWithData:message.customElem.data options:NSJSONReadingAllowFragments error:nil];return param[@"text"];}@end
contentSize:
method of the parent class to calculate the size of the drawing area occupied by the cellData
content.
Sample code:(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));// Add bubble marginssize.height += 60;size.width += 20;return size;}
TUIChat/UI_Classic/Cell/CellUI/Custom
. Inherit data from TUIMessageCell
to Cell
to draw TUILinkCellData
data.
Sample code:@interface TUILinkCell : TUIMessageCell@property UILabel *myTextLabel; // Display text@property UILabel *myLinkLabel; // Link redirection text(void)fillWithData:(TUILinkCellData *)data; // Draw UI@end
initWithStyle:reuseIdentifier:
method of the parent class to create the myTextLabel
and myLinkLabel
text display objects and add them to container
.
Sample code:@implementation TUILinkCell// 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:
method of the parent class to custom TUILinkCellData
data display in TUILinkCell
.
Sample code:@implementation TUILinkCell// Draw the cell based on cellData(void)fillWithData:(TUILinkCellData *)data;{[super fillWithData:data];self.myTextLabel.text = data.text;}@end
layoutSubviews
method of the parent class to customize the control layout.
Sample code:// 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
cell
and cellData
are created, you need to register the cell
and cellData
information in the registerExternalCustomMessageInfo function of TUIMessageCellConfig.m.
After the registration is completed, when a message is received, the message list automatically finds the corresponding cellData
to process message data based on businessID
. When refreshing the UI, the message list will also automatically create the corresponding Cell
to draw cellData
data based on businessID
.@implementation TUIMessageDataProvider+ (void)registerExternalCustomMessageInfo {// Insert your own custom message UI here, your businessID can not be same with built-in//// Example:// [self registerCustomMessageCell:#your message cell#// messageCellData:#your message cell data# forBusinessID:#your id#];//// ...}@end
@implementation TUIChatDataProvider- (NSArray<TUIInputMoreCellData *> *)customInputMoreMenus {if (_customInputMoreMenus == nil) {NSMutableArray *arrayM = [NSMutableArray array];if (TUIChatConfig.defaultConfig.enableWelcomeCustomMessage) {// Link__weak typeof(self) weakSelf = self;TUIInputMoreCellData *linkData = [[TUIInputMoreCellData alloc] init];linkData.priority = 0;linkData.title = TIMCommonLocalizableString(TUIKitMoreLink);linkData.image = TUIChatBundleThemeImage(@"chat_more_link_img", @"chat_more_link_img");linkData.onClicked = ^(NSDictionary *actionParam) {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", [weakSelf class]);return;}V2TIMMessage *message = [TUIMessageDataProvider getCustomMessageWithJsonData:data];if ([weakSelf.delegate respondsToSelector:@selector(dataProvider:sendMessage:)]) {[weakSelf.delegate dataProvider:weakSelf sendMessage:message];}};[arrayM addObject:linkData];}_customInputMoreMenus = arrayM;}return _customInputMoreMenus;}@end
Was this page helpful?