MOSystemNormalCellData.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. - (CGSize)contentSize {
  37. //文字内容显示区域
  38. CGFloat viewWidth = [[UIScreen mainScreen] bounds].size.width - 15.0 * 2.0;
  39. CGFloat contentWidth = viewWidth - 10.0 * 2.0;
  40. //标题以上固定距离
  41. CGFloat titleTopSpacing = 40.0;
  42. CGFloat pictureHeight = 0;
  43. if (self.img.length > 0) {
  44. pictureHeight = 74 + 10;//10是图片底部的间距
  45. }
  46. //标题高度
  47. CGFloat titleHeight = 0;
  48. if (self.title.length > 0) {
  49. UIFont *titleFont = [MOSystemNormalCellData getTheFontWithSize:15.0 AndFontName:@"Roboto-Bold"];
  50. titleHeight = [self.title boundingRectWithSize:CGSizeMake(contentWidth, MAXFLOAT)
  51. options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : titleFont}
  52. context:nil].size.height;
  53. }
  54. //内容高度
  55. CGFloat contentHeight = 0;
  56. if(self.content.length > 0){
  57. UIFont *contentFont = [MOSystemNormalCellData getTheFontWithSize:13.0 AndFontName:@"Roboto"];
  58. contentHeight = [self.content boundingRectWithSize:CGSizeMake(contentWidth, MAXFLOAT)
  59. options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
  60. attributes:@{NSFontAttributeName : contentFont}
  61. context:nil].size.height + 2.5;//补偿2.5的偏差
  62. }
  63. //跳转链接内容高度
  64. CGFloat linkHeight = 0;
  65. if (self.link.length > 0 && self.linkStr.length == 0) {
  66. linkHeight = 30;
  67. }
  68. //30是bgView上下距离
  69. CGFloat bgViewSpacing = 30;
  70. CGFloat totalHeigh = 0;
  71. if (self.type == 1) {//图片.标题。内容.链接
  72. totalHeigh = titleTopSpacing + pictureHeight + titleHeight + contentHeight + linkHeight + bgViewSpacing;
  73. } if (self.type == 2) {//图片.标题。内容
  74. totalHeigh = titleTopSpacing + titleHeight + pictureHeight + contentHeight + bgViewSpacing;
  75. } else if (self.type == 3) {//图片.内容
  76. totalHeigh = titleTopSpacing + pictureHeight + contentHeight + bgViewSpacing;
  77. } else if (self.type == 4) {//图片.内容。链接
  78. totalHeigh = titleTopSpacing + pictureHeight + contentHeight + linkHeight + bgViewSpacing;
  79. } else if (self.type == 5) {//标题.内容。链接
  80. totalHeigh = titleTopSpacing + titleHeight + contentHeight + linkHeight + bgViewSpacing;
  81. } else if (self.type == 6) {//内容。链接
  82. totalHeigh = titleTopSpacing + contentHeight + linkHeight + bgViewSpacing;
  83. } else if (self.type == 7) {//标题,内容,文字调整链接
  84. totalHeigh = titleTopSpacing + titleHeight + contentHeight + bgViewSpacing;
  85. } else if (self.type == 8) {//内容,文字调整链接
  86. totalHeigh = titleTopSpacing + contentHeight + bgViewSpacing;
  87. } else if (self.type == 9) {//内容
  88. totalHeigh = titleTopSpacing + contentHeight + bgViewSpacing;
  89. }
  90. CGSize size = CGSizeMake(245, ceilf(totalHeigh));
  91. return size;
  92. }
  93. + (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
  94. {
  95. id object = [dict objectForKey:aKey];
  96. return [object isEqual:[NSNull null]] ? nil : object;
  97. }
  98. + (UIFont *)getTheFontWithSize:(CGFloat)fontSize AndFontName:(NSString *)fontName{
  99. UIFont *customFont = [UIFont fontWithName:fontName size:fontSize];
  100. if(!customFont){
  101. customFont = [MOSystemNormalCellData MODisplayFontWithSize:fontSize bold:YES itatic:NO weight:UIFontWeightMedium];
  102. }
  103. return customFont;
  104. }
  105. + (UIFont *)MODisplayFontWithSize:(CGFloat)fontSize
  106. bold:(BOOL)bold itatic:(BOOL)italic weight:(UIFontWeight)weight {
  107. UIFont *font = [UIFont systemFontOfSize:fontSize weight:weight];
  108. UIFontDescriptorSymbolicTraits symbolicTraits = 0;
  109. if (italic) {
  110. symbolicTraits |= UIFontDescriptorTraitItalic;
  111. }
  112. if (bold) {
  113. symbolicTraits |= UIFontDescriptorTraitBold;
  114. }
  115. UIFont *specialFont = [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:symbolicTraits] size:font.pointSize];
  116. return specialFont;
  117. }
  118. @end