WFCUCompositeCell.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // WFCUCardCell.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/9/1.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUCompositeCell.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import "WFCUUtilities.h"
  11. #import "UILabel+YBAttributeTextTapAction.h"
  12. #import <SDWebImage/SDWebImage.h>
  13. #define TITLE_TOP_PADDING 4
  14. #define TITLE_CONTENT_PADDING 6
  15. #define CONTENT_LINE_PADDING 6
  16. #define LINE_HINT_PADDING 6
  17. #define HINT_BUTTOM_PADDING 2
  18. #define LINE_HEIGHT 1
  19. #define HINT_LABEL_HEIGHT 10
  20. #define TITLE_FONT_SIZE 18
  21. #define CONTENT_FONT_SIZE 12
  22. #define HINT_FONT_SIZE 10
  23. @interface WFCUCompositeCell ()
  24. @property (nonatomic, strong)UILabel *targetNameLabel;
  25. @property (nonatomic, strong)UILabel *contentLabel;
  26. @property (nonatomic, strong)UIView *separateLine;
  27. @property (nonatomic, strong)UILabel *hintLabel;
  28. @end
  29. @implementation WFCUCompositeCell
  30. + (CGSize)sizeForClientArea:(WFCUMessageModel *)msgModel withViewWidth:(CGFloat)width {
  31. WFCCCompositeMessageContent *content = (WFCCCompositeMessageContent *)msgModel.message.content;
  32. CGSize titleSize = [WFCUUtilities getTextDrawingSize:content.title font:[UIFont systemFontOfSize:TITLE_FONT_SIZE] constrainedSize:CGSizeMake(width, TITLE_FONT_SIZE * 3)];
  33. CGSize contentSize = [WFCUUtilities getTextDrawingSize:[WFCUCompositeCell digestContent:content inConversation:msgModel.message.conversation] font:[UIFont systemFontOfSize:CONTENT_FONT_SIZE] constrainedSize:CGSizeMake(width, 8000)];
  34. CGSize size = CGSizeMake(width, 0);
  35. size.height += TITLE_TOP_PADDING;
  36. size.height += titleSize.height;
  37. size.height += TITLE_CONTENT_PADDING;
  38. size.height += contentSize.height;
  39. size.height += CONTENT_LINE_PADDING;
  40. size.height += LINE_HEIGHT;
  41. size.height += LINE_HINT_PADDING;
  42. size.height += HINT_LABEL_HEIGHT;
  43. size.height += HINT_BUTTOM_PADDING;
  44. return size;
  45. }
  46. + (NSString *)digestContent:(WFCCCompositeMessageContent *)content inConversation:(WFCCConversation *)conversation {
  47. NSString *result = @"";
  48. for (int i = 0; i < content.messages.count; i++) {
  49. WFCCMessage *msg = content.messages[i];
  50. NSString *digest = [msg.content digest:msg];
  51. if (digest.length > 36) {
  52. digest = [digest substringToIndex:33];
  53. digest = [digest stringByAppendingString:@"..."];
  54. }
  55. WFCCUserInfo *userInfo;
  56. if (conversation.type == Group_Type) {
  57. userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:msg.fromUser inGroup:conversation.target refresh:NO];
  58. } else {
  59. userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:msg.fromUser refresh:NO];
  60. }
  61. result = [result stringByAppendingFormat:@"%@:%@", userInfo.displayName, digest];
  62. BOOL lastItem = (i == content.messages.count - 1);
  63. if (!lastItem) {
  64. result = [result stringByAppendingString:@"\n"];
  65. }
  66. if (i == 2) {
  67. if (!lastItem) {
  68. result = [result stringByAppendingString:@"..."];
  69. }
  70. break;
  71. }
  72. }
  73. return result;
  74. }
  75. - (void)setModel:(WFCUMessageModel *)model {
  76. [super setModel:model];
  77. WFCCCompositeMessageContent *content = (WFCCCompositeMessageContent *)model.message.content;
  78. self.targetNameLabel.text = content.title;
  79. self.contentLabel.text = [self.class digestContent:content inConversation:model.message.conversation];
  80. CGFloat width = self.contentArea.frame.size.width;
  81. CGSize titleSize = [WFCUUtilities getTextDrawingSize:content.title font:[UIFont systemFontOfSize:TITLE_FONT_SIZE] constrainedSize:CGSizeMake(width, TITLE_FONT_SIZE * 3)];
  82. CGSize contentSize = [WFCUUtilities getTextDrawingSize:self.contentLabel.text font:[UIFont systemFontOfSize:CONTENT_FONT_SIZE] constrainedSize:CGSizeMake(width, 8000)];
  83. int offset = TITLE_TOP_PADDING;
  84. self.targetNameLabel.frame = CGRectMake(0, offset, width, titleSize.height);
  85. offset += titleSize.height;
  86. offset += TITLE_CONTENT_PADDING;
  87. self.contentLabel.frame = CGRectMake(0, offset, width, contentSize.height);
  88. offset += contentSize.height;
  89. offset += CONTENT_LINE_PADDING;
  90. self.separateLine.frame = CGRectMake(0, offset, width, LINE_HEIGHT);
  91. offset += LINE_HEIGHT;
  92. offset += LINE_HINT_PADDING;
  93. self.hintLabel.frame = CGRectMake(0, offset, width, HINT_LABEL_HEIGHT);
  94. }
  95. - (UILabel *)targetNameLabel {
  96. if (!_targetNameLabel) {
  97. _targetNameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  98. _targetNameLabel.font = [UIFont systemFontOfSize:TITLE_FONT_SIZE];
  99. _targetNameLabel.textColor = [UIColor blackColor];
  100. _targetNameLabel.numberOfLines = 0;
  101. [self.contentArea addSubview:_targetNameLabel];
  102. }
  103. return _targetNameLabel;
  104. }
  105. - (UILabel *)contentLabel {
  106. if (!_contentLabel) {
  107. _contentLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  108. _contentLabel.font = [UIFont systemFontOfSize:CONTENT_FONT_SIZE];
  109. _contentLabel.textColor = [UIColor grayColor];
  110. _contentLabel.numberOfLines = 0;
  111. [self.contentArea addSubview:_contentLabel];
  112. }
  113. return _contentLabel;
  114. }
  115. - (UIView *)separateLine {
  116. if (!_separateLine) {
  117. _separateLine = [[UIView alloc] initWithFrame:CGRectZero];
  118. _separateLine.backgroundColor = [UIColor grayColor];
  119. [self.contentArea addSubview:_separateLine];
  120. }
  121. return _separateLine;
  122. }
  123. - (UILabel *)hintLabel {
  124. if (!_hintLabel) {
  125. _hintLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  126. _hintLabel.font = [UIFont systemFontOfSize:HINT_FONT_SIZE];
  127. _hintLabel.text = WFCString(@"ChatHistory");
  128. _hintLabel.textColor = [UIColor grayColor];
  129. [self.contentArea addSubview:_hintLabel];
  130. }
  131. return _hintLabel;
  132. }
  133. @end