TUITextMessageCellData.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // TUITextMessageCellData.m
  3. // TXIMSDK_TUIKit_iOS
  4. //
  5. // Created by annidyfeng on 2019/5/21.
  6. // Copyright © 2023 Tencent. All rights reserved.
  7. //
  8. #import "TUITextMessageCellData.h"
  9. #import <TIMCommon/NSString+TUIEmoji.h>
  10. #import <TIMCommon/TIMCommonModel.h>
  11. #import <TIMCommon/TIMDefine.h>
  12. #import <TUICore/TUIThemeManager.h>
  13. #ifndef CGFLOAT_CEIL
  14. #ifdef CGFLOAT_IS_DOUBLE
  15. #define CGFLOAT_CEIL(value) ceil(value)
  16. #else
  17. #define CGFLOAT_CEIL(value) ceilf(value)
  18. #endif
  19. #endif
  20. @interface TUITextMessageCellData ()
  21. @property(nonatomic, assign) CGSize size;
  22. @property(nonatomic, assign) CGFloat containerWidth;
  23. @property(nonatomic, strong) NSMutableAttributedString *attributedString;
  24. @end
  25. @implementation TUITextMessageCellData
  26. {
  27. NSString *_content;
  28. }
  29. + (TUIMessageCellData *)getCellData:(V2TIMMessage *)message {
  30. TUITextMessageCellData *textData = [[TUITextMessageCellData alloc] initWithDirection:(message.isSelf ? MsgDirectionOutgoing : MsgDirectionIncoming)];
  31. textData.content = message.textElem.text;
  32. textData.reuseId = TTextMessageCell_ReuseId;
  33. textData.status = Msg_Status_Init;
  34. return textData;
  35. }
  36. + (NSString *)getDisplayString:(V2TIMMessage *)message {
  37. NSString *content = message.textElem.text;
  38. return content.getLocalizableStringWithFaceContent;
  39. }
  40. - (Class)getReplyQuoteViewDataClass {
  41. return NSClassFromString(@"TUITextReplyQuoteViewData");
  42. }
  43. - (Class)getReplyQuoteViewClass {
  44. return NSClassFromString(@"TUITextReplyQuoteView");
  45. }
  46. - (instancetype)initWithDirection:(TMsgDirection)direction {
  47. self = [super initWithDirection:direction];
  48. if (self) {
  49. if (direction == MsgDirectionIncoming) {
  50. self.cellLayout = [TUIMessageCellLayout incommingTextMessageLayout];
  51. } else {
  52. self.cellLayout = [TUIMessageCellLayout outgoingTextMessageLayout];
  53. }
  54. }
  55. return self;
  56. }
  57. - (void)setContent:(NSString *)content {
  58. if (![_content isEqualToString:content]) {
  59. _content = content;
  60. _attributedString = nil;
  61. }
  62. }
  63. - (NSString *)content {
  64. return _content;
  65. }
  66. - (NSAttributedString *)getContentAttributedString:(UIFont *)textFont {
  67. if (!_attributedString) {
  68. _emojiLocations = [NSMutableArray array];
  69. _attributedString = [self.content getFormatEmojiStringWithFont:textFont emojiLocations:_emojiLocations];
  70. if (self.isAudioCall || self.isVideoCall) {
  71. NSTextAttachment *attchment = [[NSTextAttachment alloc] init];
  72. UIImage *image = nil;
  73. if (self.isAudioCall) {
  74. image = TUIChatCommonBundleImage(@"audio_call");
  75. }
  76. if (self.isVideoCall) {
  77. if (self.isCaller) {
  78. image = TUIChatCommonBundleImage(@"video_call_self");
  79. } else {
  80. image = TUIChatCommonBundleImage(@"video_call");
  81. }
  82. }
  83. attchment.image = image;
  84. attchment.bounds = CGRectMake(0, -(textFont.lineHeight - textFont.pointSize) / 2, 16, 16);
  85. NSAttributedString *imageString = [NSAttributedString attributedStringWithAttachment:(NSTextAttachment *)(attchment)];
  86. NSAttributedString *spaceString = [[NSAttributedString alloc] initWithString:@" " attributes:@{NSFontAttributeName : textFont}];
  87. if (self.isCaller) {
  88. [_attributedString appendAttributedString:spaceString];
  89. [_attributedString appendAttributedString:imageString];
  90. } else {
  91. [_attributedString insertAttributedString:spaceString atIndex:0];
  92. [_attributedString insertAttributedString:imageString atIndex:0];
  93. }
  94. }
  95. }
  96. return _attributedString;
  97. }
  98. - (CGSize)getContentAttributedStringSize:(NSAttributedString *)attributeString maxTextSize:(CGSize)maxTextSize {
  99. CGRect rect = [attributeString boundingRectWithSize:maxTextSize
  100. options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
  101. context:nil];
  102. CGFloat width = CGFLOAT_CEIL(rect.size.width);
  103. CGFloat height = CGFLOAT_CEIL(rect.size.height);
  104. return CGSizeMake(width, height);
  105. }
  106. @end