WFCUArticlesCell.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // WFCUArticlesCell.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/9/1.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUArticlesCell.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import "WFCUUtilities.h"
  11. #import "UILabel+YBAttributeTextTapAction.h"
  12. #import <SDWebImage/SDWebImage.h>
  13. #import "UIColor+YH.h"
  14. @interface WFCUArticlesCell ()
  15. @property(nonatomic, strong)UIView *containerView;
  16. @property(nonatomic, strong)UIImageView *coverImageView;
  17. @property(nonatomic, strong)UILabel *topTitleLabel;
  18. @property(nonatomic, strong)NSMutableArray<UIView *> *itemViews;
  19. @property(nonatomic, strong)UIView *subArticleViewContainer;
  20. @end
  21. //CELL左右的margin
  22. #define CELL_MARGIN 32
  23. //CELL上下的margin
  24. #define CELL_MARGIN_TOP 8
  25. #define CELL_MARGIN_BUTTOM 16
  26. //Cell的左右padding
  27. #define CELL_PADDING 16
  28. //Cell的上部的padding
  29. #define CELL_PADDING_TOP 8
  30. //Cell的底部的padding
  31. #define CELL_PADDING_BUTTOM 12
  32. //描述和Item的padding
  33. #define CELL_DESC_ITEM_PADDING 16
  34. //Item之间的padding
  35. #define CELL_ITEM_PADDING 8
  36. //Item和下部line的padding
  37. #define CELL_ITEM_LINE_PADDING 4
  38. //Key Label的宽度
  39. #define KEY_WIDTH 80
  40. //Value Label距离左边侧的Padding(包括KEY_WIDTH)
  41. #define VALUE_BOARD_PADDING_LEFT 86
  42. //Value Label距离右边侧的Padding
  43. #define VALUE_BOARD_PADDING_RIGHT CELL_PADDING
  44. #define EX_LINE_WIDTH 0.5
  45. #define TOP_TITLE_FONT_SIZE 16
  46. #define TITLE_FONT_SIZE 16
  47. #define FONT_SIZE 14
  48. #define EX_FONT_SIZE 14
  49. #define EX_FW_WIDTH 28
  50. #define SUB_COVER_SIZE 44
  51. #define SUB_TITLE_FONT_SIZE 14
  52. #define COVER_HW_RATE 0.35
  53. @implementation WFCUArticlesCell
  54. + (CGSize)sizeForCell:(WFCUMessageModel *)msgModel withViewWidth:(CGFloat)width {
  55. WFCCArticlesMessageContent *content = (WFCCArticlesMessageContent *)msgModel.message.content;
  56. CGFloat containerWidth = [UIScreen mainScreen].bounds.size.width - CELL_MARGIN - CELL_MARGIN;
  57. CGFloat coverHeight = containerWidth * COVER_HW_RATE;
  58. CGFloat labHeight = 0;
  59. if(content.subArticles.count) {
  60. labHeight = (SUB_COVER_SIZE + CELL_ITEM_PADDING) * content.subArticles.count;
  61. } else {
  62. CGSize titleSize = [WFCUUtilities getTextDrawingSize:content.topArticle.title font:[UIFont systemFontOfSize:TOP_TITLE_FONT_SIZE] constrainedSize:CGSizeMake(containerWidth-CELL_PADDING-CELL_PADDING, 50)];
  63. labHeight = CELL_ITEM_PADDING;
  64. labHeight += titleSize.height;
  65. }
  66. return CGSizeMake(width, CELL_MARGIN_TOP + CELL_PADDING_TOP + coverHeight + labHeight + CELL_PADDING_BUTTOM + CELL_MARGIN_BUTTOM);
  67. }
  68. - (void)setModel:(WFCUMessageModel *)model {
  69. [super setModel:model];
  70. [self containerView];
  71. WFCCArticlesMessageContent *content = (WFCCArticlesMessageContent *)model.message.content;
  72. CGFloat containerWidth = [UIScreen mainScreen].bounds.size.width - CELL_MARGIN - CELL_MARGIN;
  73. __block CGFloat offset = CELL_PADDING_TOP;
  74. [self removeAllItems];
  75. [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:content.topArticle.cover]];
  76. offset += self.coverImageView.frame.size.height;
  77. offset += CELL_ITEM_PADDING;
  78. CGRect frame = self.topTitleLabel.frame;
  79. CGSize titleSize = [WFCUUtilities getTextDrawingSize:content.topArticle.title font:[UIFont systemFontOfSize:TOP_TITLE_FONT_SIZE] constrainedSize:CGSizeMake(containerWidth-CELL_PADDING-CELL_PADDING, 50)];
  80. frame.size.height = titleSize.height;
  81. if(content.subArticles.count) {
  82. frame.origin.y = self.coverImageView.frame.size.height - titleSize.height - CELL_ITEM_PADDING;
  83. [content.subArticles enumerateObjectsUsingBlock:^(WFCCArticle * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  84. CGFloat height = [self addArticle:obj offset:offset containerWidth:containerWidth index:idx];
  85. offset += height;
  86. offset += CELL_ITEM_PADDING;
  87. }];
  88. offset -= CELL_ITEM_PADDING;
  89. } else {
  90. frame.origin.y = offset;
  91. offset += frame.size.height;
  92. }
  93. self.topTitleLabel.frame = frame;
  94. self.topTitleLabel.text = content.topArticle.title;
  95. offset += CELL_PADDING_BUTTOM;
  96. frame = self.containerView.frame;
  97. frame.size.height = offset;
  98. self.containerView.frame = frame;
  99. }
  100. - (CGFloat)addArticle:(WFCCArticle *)article offset:(CGFloat)offset containerWidth:(CGFloat)containerWidth index:(NSUInteger)index {
  101. UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, offset, containerWidth, SUB_COVER_SIZE)];
  102. container.tag = index;
  103. [self.itemViews addObject:container];
  104. [self.containerView addSubview:container];
  105. UILabel *subTitle = [[UILabel alloc] initWithFrame:CGRectMake(CELL_PADDING, (SUB_COVER_SIZE - SUB_TITLE_FONT_SIZE - SUB_TITLE_FONT_SIZE)/2, containerWidth - CELL_PADDING*3 - SUB_COVER_SIZE, SUB_TITLE_FONT_SIZE * 2)];
  106. subTitle.numberOfLines = 2;
  107. subTitle.font = [UIFont systemFontOfSize:SUB_TITLE_FONT_SIZE];
  108. subTitle.text = article.title;
  109. [container addSubview:subTitle];
  110. UIImageView *subCover = [[UIImageView alloc] initWithFrame:CGRectMake(containerWidth - CELL_PADDING - SUB_COVER_SIZE, 0, SUB_COVER_SIZE, SUB_COVER_SIZE)];
  111. [subCover sd_setImageWithURL:[NSURL URLWithString:article.cover]];
  112. [container addSubview:subCover];
  113. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapArticle:)];
  114. [container addGestureRecognizer:tap];
  115. tap.cancelsTouchesInView = NO;
  116. [container setUserInteractionEnabled:YES];
  117. return SUB_COVER_SIZE;
  118. }
  119. - (void)didTapCell:(UITapGestureRecognizer *)tap {
  120. WFCCArticlesMessageContent *content = (WFCCArticlesMessageContent *)self.model.message.content;
  121. WFCCArticle *article = content.topArticle;
  122. if ([self.delegate respondsToSelector:@selector(didTapArticleCell:withModel:withArticle:)]) {
  123. [self.delegate didTapArticleCell:self withModel:self.model withArticle:article];
  124. }
  125. }
  126. - (void)didTapArticle:(UITapGestureRecognizer *)tap {
  127. WFCCArticlesMessageContent *content = (WFCCArticlesMessageContent *)self.model.message.content;
  128. WFCCArticle *article = content.subArticles[tap.view.tag];
  129. if ([self.delegate respondsToSelector:@selector(didTapArticleCell:withModel:withArticle:)]) {
  130. [self.delegate didTapArticleCell:self withModel:self.model withArticle:article];
  131. }
  132. }
  133. - (void)removeAllItems {
  134. [self.itemViews enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  135. [obj removeFromSuperview];
  136. }];
  137. [self.itemViews removeAllObjects];
  138. }
  139. - (CGFloat)setLabel:(UILabel *)label widht:(CGFloat)width text:(NSString *)text offset:(CGFloat)offset fontSize:(int)fontSize {
  140. CGSize titleSize = [WFCUUtilities getTextDrawingSize:text font:[UIFont systemFontOfSize:fontSize] constrainedSize:CGSizeMake(width, 50)];
  141. label.text = text;
  142. CGRect frame = label.frame;
  143. frame.origin.y = offset;
  144. frame.size.height = titleSize.height;
  145. label.frame = frame;
  146. label.numberOfLines = 0;
  147. return titleSize.height;
  148. }
  149. -(NSMutableArray<UIView *> *)itemViews {
  150. if(!_itemViews) {
  151. _itemViews = [[NSMutableArray alloc] init];
  152. }
  153. return _itemViews;
  154. }
  155. - (UIView *)containerView {
  156. if(!_containerView) {
  157. CGFloat containerWidth = [UIScreen mainScreen].bounds.size.width - CELL_MARGIN - CELL_MARGIN;
  158. _containerView = [[UIView alloc] initWithFrame:CGRectMake(CELL_MARGIN, CELL_MARGIN_TOP, containerWidth, 0)];
  159. _containerView.backgroundColor = [UIColor whiteColor];
  160. _containerView.layer.masksToBounds = YES;
  161. _containerView.layer.cornerRadius = 5.f;
  162. UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onDoubleTaped:)];
  163. doubleTapGesture.numberOfTapsRequired = 2;
  164. doubleTapGesture.numberOfTouchesRequired = 1;
  165. [_containerView addGestureRecognizer:doubleTapGesture];
  166. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapCell:)];
  167. [tap requireGestureRecognizerToFail:doubleTapGesture];
  168. tap.cancelsTouchesInView = NO;
  169. [_containerView addGestureRecognizer:tap];
  170. [_containerView addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onLongPressed:)]];
  171. [_containerView setUserInteractionEnabled:YES];
  172. _coverImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, containerWidth, containerWidth * COVER_HW_RATE)];
  173. [_containerView addSubview:_coverImageView];
  174. _topTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(CELL_PADDING, 0, containerWidth - CELL_PADDING - CELL_PADDING, TOP_TITLE_FONT_SIZE * 2)];
  175. _topTitleLabel.numberOfLines = 2;
  176. _topTitleLabel.font = [UIFont systemFontOfSize:TOP_TITLE_FONT_SIZE];
  177. [_containerView addSubview:_topTitleLabel];
  178. [self.contentView addSubview:_containerView];
  179. }
  180. return _containerView;
  181. }
  182. @end