// // TUITextMessageCellData.m // TXIMSDK_TUIKit_iOS // // Created by annidyfeng on 2019/5/21. // #import "TUITextMessageCellData.h" #import "TUIFaceView.h" #import "TUICommonModel.h" #import "TUIDefine.h" #import "NSString+emoji.h" #import "TUIThemeManager.h" #ifndef CGFLOAT_CEIL #ifdef CGFLOAT_IS_DOUBLE #define CGFLOAT_CEIL(value) ceil(value) #else #define CGFLOAT_CEIL(value) ceilf(value) #endif #endif @interface TUITextMessageCellData() @property CGSize textSize; @property CGPoint textOrigin; @end @implementation TUITextMessageCellData + (void)initialize { [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(onThemeChanged) name:TUIDidApplyingThemeChangedNotfication object:nil]; } + (TUIMessageCellData *)getCellData:(V2TIMMessage *)message { TUITextMessageCellData *textData = [[TUITextMessageCellData alloc] initWithDirection:(message.isSelf ? MsgDirectionOutgoing : MsgDirectionIncoming)]; textData.content = message.textElem.text; textData.reuseId = TTextMessageCell_ReuseId; return textData; } + (NSString *)getDisplayString:(V2TIMMessage *)message { // 处理表情的国际化 NSString *content = message.textElem.text; return content.getLocalizableStringWithFaceContent; } - (Class)getReplyQuoteViewDataClass { return NSClassFromString(@"TUITextReplyQuoteViewData"); } - (Class)getReplyQuoteViewClass { return NSClassFromString(@"TUITextReplyQuoteView"); } - (instancetype)initWithDirection:(TMsgDirection)direction { self = [super initWithDirection:direction]; if (self) { if (direction == MsgDirectionIncoming) { _textColor = [[self class] incommingTextColor]; _textFont = [[self class] incommingTextFont]; self.cellLayout = [TUIMessageCellLayout incommingTextMessageLayout]; } else { _textColor = [[self class] outgoingTextColor]; _textFont = [[self class] outgoingTextFont]; self.cellLayout = [TUIMessageCellLayout outgoingTextMessageLayout]; } } return self; } - (CGSize)contentSize { CGRect rect = [self.attributedString boundingRectWithSize:CGSizeMake(TTextMessageCell_Text_Width_Max, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil]; CGSize size = CGSizeMake(CGFLOAT_CEIL(rect.size.width), CGFLOAT_CEIL(rect.size.height)); self.textSize = size; self.textOrigin = CGPointMake(self.cellLayout.bubbleInsets.left, self.cellLayout.bubbleInsets.top+self.bubbleTop); size.height += self.cellLayout.bubbleInsets.top+self.cellLayout.bubbleInsets.bottom; size.width += self.cellLayout.bubbleInsets.left+self.cellLayout.bubbleInsets.right; if (self.direction == MsgDirectionIncoming) { // size.width = MAX(size.width, [TUIBubbleMessageCellData incommingBubble].size.width); size.height = MAX(size.height, [TUIBubbleMessageCellData incommingBubble].size.height); } else { // size.width = MAX(size.width, [TUIBubbleMessageCellData outgoingBubble].size.width); size.height = MAX(size.height, [TUIBubbleMessageCellData outgoingBubble].size.height); } return size; } - (NSMutableAttributedString *)attributedString { if (!_attributedString) { _emojiLocations = [NSMutableArray array]; _attributedString = [self.content getFormatEmojiStringWithFont:self.textFont emojiLocations:_emojiLocations]; // 音视频通话消息需要加上对应图标 if (self.isAudioCall || self.isVideoCall) { NSTextAttachment *attchment = [[NSTextAttachment alloc] init]; UIImage *image = nil; if (self.isAudioCall) { image = [UIImage d_imagePath:TUIChatImagePath(@"audio_call")]; } if (self.isVideoCall) { if (self.innerMessage.isSelf) { image = [UIImage d_imagePath:TUIChatImagePath(@"video_call_self")]; } else { image = [UIImage d_imagePath:TUIChatImagePath(@"video_call")]; } } attchment.image = image; attchment.bounds = CGRectMake(0, -(self.textFont.lineHeight-self.textFont.pointSize)/2, self.textFont.pointSize, self.textFont.pointSize); NSAttributedString *imageString = [NSAttributedString attributedStringWithAttachment:(NSTextAttachment *)(attchment)]; NSAttributedString *spaceString = [[NSAttributedString alloc] initWithString:@" " attributes:@{NSFontAttributeName: self.textFont}]; if (self.innerMessage.isSelf) { [_attributedString appendAttributedString:spaceString]; [_attributedString appendAttributedString:imageString]; } else { [_attributedString insertAttributedString:spaceString atIndex:0]; [_attributedString insertAttributedString:imageString atIndex:0]; } } } return _attributedString; } static UIColor *sOutgoingTextColor; + (UIColor *)outgoingTextColor { if (!sOutgoingTextColor) { sOutgoingTextColor = TUIChatDynamicColor(@"chat_text_message_send_text_color", @"#000000"); } return sOutgoingTextColor; } + (void)setOutgoingTextColor:(UIColor *)outgoingTextColor { sOutgoingTextColor = outgoingTextColor; } static UIFont *sOutgoingTextFont; + (UIFont *)outgoingTextFont { if (!sOutgoingTextFont) { sOutgoingTextFont = [UIFont systemFontOfSize:16]; } return sOutgoingTextFont; } + (void)setOutgoingTextFont:(UIFont *)outgoingTextFont { sOutgoingTextFont = outgoingTextFont; } static UIColor *sIncommingTextColor; + (UIColor *)incommingTextColor { if (!sIncommingTextColor) { sIncommingTextColor = TUIChatDynamicColor(@"chat_text_message_receive_text_color", @"#000000"); } return sIncommingTextColor; } + (void)setIncommingTextColor:(UIColor *)incommingTextColor { sIncommingTextColor = incommingTextColor; } static UIFont *sIncommingTextFont; + (UIFont *)incommingTextFont { if (!sIncommingTextFont) { sIncommingTextFont = [UIFont systemFontOfSize:16]; } return sIncommingTextFont; } + (void)setIncommingTextFont:(UIFont *)incommingTextFont { sIncommingTextFont = incommingTextFont; } + (void)onThemeChanged { sOutgoingTextColor = nil; sIncommingTextColor = nil; } @end