MOSystemNormalCellData.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // MOSystemNormalCellData.m
  3. // TUIConversation
  4. //
  5. // Created by SuperC on 2025/5/16.
  6. //
  7. #import "MOSystemNormalCellData.h"
  8. @implementation MOSystemNormalCellData
  9. + (TUIMessageCellData *)getCellData:(V2TIMMessage *)message {
  10. NSDictionary *param = [NSJSONSerialization JSONObjectWithData:message.customElem.data options:NSJSONReadingAllowFragments error:nil];
  11. if (param == nil) {
  12. return nil;
  13. }
  14. MOSystemNormalCellData *cellData = [[MOSystemNormalCellData alloc] initWithDirection:message.isSelf ? MsgDirectionOutgoing : MsgDirectionIncoming];
  15. cellData.reuseId = @"MOSystemNormalCell";
  16. cellData.innerMessage = message;
  17. cellData.type = [[MOSystemNormalCellData objectOrNilForKey:@"type" fromDictionary:param] integerValue];
  18. cellData.img = [MOSystemNormalCellData objectOrNilForKey:@"img" fromDictionary:param];
  19. cellData.title = [MOSystemNormalCellData objectOrNilForKey:@"title" fromDictionary:param];
  20. cellData.content = [MOSystemNormalCellData objectOrNilForKey:@"content" fromDictionary:param];
  21. cellData.link = [MOSystemNormalCellData objectOrNilForKey:@"link" fromDictionary:param];
  22. cellData.linkStr = [MOSystemNormalCellData objectOrNilForKey:@"linkStr" fromDictionary:param];
  23. return cellData;
  24. }
  25. + (NSString *)getDisplayString:(V2TIMMessage *)message {
  26. NSDictionary *param = [NSJSONSerialization JSONObjectWithData:message.customElem.data options:NSJSONReadingAllowFragments error:nil];
  27. if (param == nil) {
  28. return nil;
  29. }
  30. NSString *contentStr = [MOSystemNormalCellData objectOrNilForKey:@"content" fromDictionary:param];
  31. if(contentStr.length > 0){
  32. return contentStr;
  33. }
  34. return message.customElem.desc;
  35. }
  36. - (BOOL)showPicture {
  37. if (self.type == 1) {//图片.标题。内容.链接
  38. return YES;
  39. } if (self.type == 2) {//图片.标题。内容
  40. return YES;
  41. } else if (self.type == 3) {//图片.内容
  42. return YES;
  43. } else if (self.type == 4) {//图片.内容。链接
  44. return YES;
  45. } else if (self.type == 5) {//标题.内容。链接
  46. return NO;
  47. } else if (self.type == 6) {//内容。链接
  48. return NO;
  49. } else if (self.type == 7) {//标题,内容,文字调整链接
  50. return NO;
  51. } else if (self.type == 8) {//内容,文字调整链接
  52. return NO;
  53. } else if (self.type == 9) {//内容
  54. return NO;
  55. }
  56. return NO;
  57. }
  58. - (BOOL)showTitle {
  59. if (self.type == 1) {//图片.标题。内容.链接
  60. return YES;
  61. } if (self.type == 2) {//图片.标题。内容
  62. return YES;
  63. } else if (self.type == 3) {//图片.内容
  64. return NO;
  65. } else if (self.type == 4) {//图片.内容。链接
  66. return NO;
  67. } else if (self.type == 5) {//标题.内容。链接
  68. return YES;
  69. } else if (self.type == 6) {//内容。链接
  70. return NO;
  71. } else if (self.type == 7) {//标题,内容,文字调整链接
  72. return YES;
  73. } else if (self.type == 8) {//内容,文字调整链接
  74. return NO;
  75. } else if (self.type == 9) {//内容
  76. return NO;
  77. }
  78. return NO;
  79. }
  80. - (BOOL)showDetailsView {
  81. if (self.type == 1) {//图片.标题。内容.链接
  82. return YES;
  83. } if (self.type == 2) {//图片.标题。内容
  84. return NO;
  85. } else if (self.type == 3) {//图片.内容
  86. return NO;
  87. } else if (self.type == 4) {//图片.内容。链接
  88. return YES;
  89. } else if (self.type == 5) {//标题.内容。链接
  90. return YES;
  91. } else if (self.type == 6) {//内容。链接
  92. return YES;
  93. } else if (self.type == 7) {//标题,内容,文字调整链接
  94. return NO;
  95. } else if (self.type == 8) {//内容,文字调整链接
  96. return NO;
  97. } else if (self.type == 9) {//内容
  98. return NO;
  99. }
  100. return NO;
  101. }
  102. - (CGSize)contentSize {
  103. //文字内容显示区域
  104. CGFloat viewWidth = [[UIScreen mainScreen] bounds].size.width - 15.0 * 2.0;
  105. CGFloat contentWidth = viewWidth - 10.0 * 2.0;
  106. //标题以上固定距离
  107. CGFloat titleTopSpacing = 40.0;
  108. CGFloat pictureHeight = 0;
  109. if (self.img.length > 0 && [self showPicture]) {
  110. pictureHeight = 74 + 10;//10是图片底部的间距
  111. }
  112. //标题高度
  113. CGFloat titleHeight = 0;
  114. if (self.title.length > 0 && [self showTitle]) {
  115. UIFont *titleFont = [MOSystemNormalCellData getTheFontWithSize:15.0 AndFontName:@"Roboto-Bold"];
  116. titleHeight = [self.title boundingRectWithSize:CGSizeMake(contentWidth, MAXFLOAT)
  117. options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : titleFont}
  118. context:nil].size.height + 2;//2是补偿高度
  119. }
  120. //内容高度
  121. CGFloat contentHeight = 0;
  122. if(self.content.length > 0){
  123. UIFont *contentFont = [MOSystemNormalCellData getTheFontWithSize:13.0 AndFontName:@"Roboto"];
  124. contentHeight = [self.content boundingRectWithSize:CGSizeMake(contentWidth, MAXFLOAT)
  125. options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
  126. attributes:@{NSFontAttributeName : contentFont}
  127. context:nil].size.height;
  128. }
  129. //跳转链接内容高度
  130. CGFloat linkHeight = 0;
  131. if (self.link.length > 0 && [self showDetailsView]) {
  132. linkHeight = 30;
  133. }
  134. //30是bgView上下距离
  135. CGFloat bgViewSpacing = 30;
  136. CGFloat totalHeigh = 0;
  137. if (self.type == 1) {//图片.标题。内容.链接
  138. totalHeigh = titleTopSpacing + pictureHeight + titleHeight + contentHeight + linkHeight + bgViewSpacing;
  139. } if (self.type == 2) {//图片.标题。内容
  140. totalHeigh = titleTopSpacing + titleHeight + pictureHeight + contentHeight + bgViewSpacing;
  141. } else if (self.type == 3) {//图片.内容
  142. totalHeigh = titleTopSpacing + pictureHeight + contentHeight + bgViewSpacing;
  143. } else if (self.type == 4) {//图片.内容。链接
  144. totalHeigh = titleTopSpacing + pictureHeight + contentHeight + linkHeight + bgViewSpacing;
  145. } else if (self.type == 5) {//标题.内容。链接
  146. totalHeigh = titleTopSpacing + titleHeight + contentHeight + linkHeight + bgViewSpacing;
  147. } else if (self.type == 6) {//内容。链接
  148. totalHeigh = titleTopSpacing + contentHeight + linkHeight + bgViewSpacing;
  149. } else if (self.type == 7) {//标题,内容,文字调整链接
  150. totalHeigh = titleTopSpacing + titleHeight + contentHeight + bgViewSpacing;
  151. } else if (self.type == 8) {//内容,文字调整链接
  152. totalHeigh = titleTopSpacing + contentHeight + bgViewSpacing;
  153. } else if (self.type == 9) {//内容
  154. totalHeigh = titleTopSpacing + contentHeight + bgViewSpacing - 5;//这个类型有5的偏差,直接这里改好了
  155. }
  156. CGSize size = CGSizeMake(245, ceilf(totalHeigh));
  157. return size;
  158. }
  159. + (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
  160. {
  161. id object = [dict objectForKey:aKey];
  162. return [object isEqual:[NSNull null]] ? nil : object;
  163. }
  164. + (UIFont *)getTheFontWithSize:(CGFloat)fontSize AndFontName:(NSString *)fontName{
  165. UIFont *customFont = [UIFont fontWithName:fontName size:fontSize];
  166. if(!customFont){
  167. customFont = [MOSystemNormalCellData MODisplayFontWithSize:fontSize bold:YES itatic:NO weight:UIFontWeightMedium];
  168. }
  169. return customFont;
  170. }
  171. + (UIFont *)MODisplayFontWithSize:(CGFloat)fontSize
  172. bold:(BOOL)bold itatic:(BOOL)italic weight:(UIFontWeight)weight {
  173. UIFont *font = [UIFont systemFontOfSize:fontSize weight:weight];
  174. UIFontDescriptorSymbolicTraits symbolicTraits = 0;
  175. if (italic) {
  176. symbolicTraits |= UIFontDescriptorTraitItalic;
  177. }
  178. if (bold) {
  179. symbolicTraits |= UIFontDescriptorTraitBold;
  180. }
  181. UIFont *specialFont = [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:symbolicTraits] size:font.pointSize];
  182. return specialFont;
  183. }
  184. @end