WFCURichNotificationCell.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //
  2. // WFCURichNotificationCell.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/9/1.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCURichNotificationCell.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. #import "WFCUImage.h"
  15. @interface WFCURichNotificationCell ()
  16. @property(nonatomic, strong)UIView *containerView;
  17. @property(nonatomic, strong)UILabel *titleLabel;
  18. @property(nonatomic, strong)UILabel *descLabel;
  19. @property(nonatomic, strong)NSMutableArray<UIView *> *itemViews;
  20. @property(nonatomic, strong)UILabel *remarkLabel;
  21. @property(nonatomic, strong)UIView *exView;
  22. @property(nonatomic, strong)UIView *exLine;
  23. @property(nonatomic, strong)UILabel *exName;
  24. @property(nonatomic, strong)UIImageView *exFWView;
  25. @property(nonatomic, strong)UIImageView *exPortraitView;
  26. @end
  27. //CELL左右的margin
  28. #define CELL_MARGIN 32
  29. //CELL上下的margin
  30. #define CELL_MARGIN_TOP_BUTTOM 8
  31. //Cell的左右padding
  32. #define CELL_PADDING 16
  33. //Cell的上部的padding
  34. #define CELL_PADDING_TOP 8
  35. //Cell的底部的padding
  36. #define CELL_PADDING_BUTTOM 8
  37. //描述和Item的padding
  38. #define CELL_DESC_ITEM_PADDING 16
  39. //Item之间的padding
  40. #define CELL_ITEM_PADDING 8
  41. //Item和下部line的padding
  42. #define CELL_ITEM_LINE_PADDING 4
  43. //Key Label的宽度
  44. #define KEY_WIDTH 80
  45. //Value Label距离左边侧的Padding(包括KEY_WIDTH)
  46. #define VALUE_BOARD_PADDING_LEFT 86
  47. //Value Label距离右边侧的Padding
  48. #define VALUE_BOARD_PADDING_RIGHT CELL_PADDING
  49. #define EX_LINE_WIDTH 0.5
  50. #define TITLE_FONT_SIZE 16
  51. #define FONT_SIZE 14
  52. #define EX_FONT_SIZE 14
  53. #define EX_FW_WIDTH 28
  54. @implementation WFCURichNotificationCell
  55. + (CGSize)sizeForCell:(WFCUMessageModel *)msgModel withViewWidth:(CGFloat)width {
  56. WFCCRichNotificationMessageContent *content = (WFCCRichNotificationMessageContent *)msgModel.message.content;
  57. CGFloat containerWidth = [UIScreen mainScreen].bounds.size.width - CELL_MARGIN - CELL_MARGIN;
  58. CGSize titleSize = [WFCUUtilities getTextDrawingSize:content.title font:[UIFont systemFontOfSize:TITLE_FONT_SIZE] constrainedSize:CGSizeMake(containerWidth-CELL_PADDING-CELL_PADDING, 50)];
  59. CGSize descSize = [WFCUUtilities getTextDrawingSize:content.desc font:[UIFont systemFontOfSize:FONT_SIZE] constrainedSize:CGSizeMake(containerWidth-CELL_PADDING-CELL_PADDING, 50)];
  60. CGFloat itemsHeight = 0;
  61. for (NSDictionary<NSString*, NSString*> *data in content.datas) {
  62. NSString *value = data[@"value"];
  63. CGSize itemSize = [WFCUUtilities getTextDrawingSize:value font:[UIFont systemFontOfSize:FONT_SIZE] constrainedSize:CGSizeMake(containerWidth-CELL_PADDING-CELL_PADDING-VALUE_BOARD_PADDING_LEFT, 50)];
  64. itemsHeight += itemSize.height;
  65. itemsHeight += CELL_ITEM_PADDING;
  66. }
  67. if(content.datas.count) {
  68. itemsHeight -= CELL_ITEM_PADDING;
  69. }
  70. itemsHeight += CELL_ITEM_LINE_PADDING;
  71. CGSize remarkSize = [WFCUUtilities getTextDrawingSize:content.remark font:[UIFont systemFontOfSize:FONT_SIZE] constrainedSize:CGSizeMake(containerWidth-CELL_PADDING-CELL_PADDING, 50)];
  72. CGSize exSize = CGSizeMake(containerWidth-CELL_PADDING-CELL_PADDING, content.exName.length ? EX_FONT_SIZE + CELL_ITEM_PADDING + CELL_PADDING_BUTTOM + EX_LINE_WIDTH : 0);
  73. CGFloat height = CELL_MARGIN_TOP_BUTTOM + CELL_PADDING_TOP + titleSize.height + CELL_ITEM_PADDING + descSize.height + CELL_DESC_ITEM_PADDING + itemsHeight + (remarkSize.height > 0 ? remarkSize.height + CELL_ITEM_PADDING : 0) + exSize.height + CELL_PADDING_BUTTOM + CELL_MARGIN_TOP_BUTTOM;
  74. return CGSizeMake(width, height);
  75. }
  76. - (void)setModel:(WFCUMessageModel *)model {
  77. [super setModel:model];
  78. [self removeAllItems];
  79. WFCCRichNotificationMessageContent *content = (WFCCRichNotificationMessageContent *)model.message.content;
  80. CGFloat containerWidth = [UIScreen mainScreen].bounds.size.width - CELL_MARGIN - CELL_MARGIN;
  81. CGFloat offset = CELL_PADDING_TOP;
  82. CGFloat height = [self setLabel:self.titleLabel widht:containerWidth-CELL_PADDING-CELL_PADDING text:content.title offset:offset fontSize:TITLE_FONT_SIZE];
  83. offset += height;
  84. offset += CELL_ITEM_PADDING;
  85. height = [self setLabel:self.descLabel widht:containerWidth-CELL_PADDING-CELL_PADDING text:content.desc offset:offset fontSize:FONT_SIZE];
  86. offset += height;
  87. offset += CELL_DESC_ITEM_PADDING;
  88. for (NSDictionary<NSString*, NSString*> *data in content.datas) {
  89. NSString *key = data[@"key"];
  90. NSString *value = data[@"value"];
  91. NSString *colorStr = data[@"color"];
  92. [self addKeyLabel:offset text:key];
  93. height = [self addValueLabel:offset text:value color:colorStr];
  94. offset += height;
  95. offset += CELL_ITEM_PADDING;
  96. }
  97. if(content.remark.length) {
  98. height = [self setLabel:self.remarkLabel widht:containerWidth-CELL_PADDING-CELL_PADDING text:content.remark offset:offset fontSize:FONT_SIZE];
  99. offset += height;
  100. offset += CELL_DESC_ITEM_PADDING;
  101. self.remarkLabel.hidden = NO;
  102. } else {
  103. self.remarkLabel.hidden = YES;
  104. }
  105. if(content.datas.count) {
  106. offset -= CELL_ITEM_PADDING;
  107. }
  108. offset += CELL_ITEM_LINE_PADDING;
  109. if(content.exName.length) {
  110. self.exView.hidden = NO;
  111. self.exName.text = content.exName;
  112. CGRect frame = self.exView.frame;
  113. frame.origin.y = offset;
  114. self.exView.frame = frame;
  115. [self.exPortraitView sd_setImageWithURL:[NSURL URLWithString:content.exPortrait] placeholderImage:[WFCUImage imageNamed:@"default_app_icon"]];
  116. offset += self.exView.frame.size.height;
  117. } else {
  118. self.exView.hidden = YES;
  119. }
  120. CGRect frame = self.containerView.frame;
  121. frame.size.height = offset;
  122. self.containerView.frame = frame;
  123. }
  124. - (void)addKeyLabel:(CGFloat)offset text:(NSString *)text {
  125. UILabel *keyLabel = [[UILabel alloc] initWithFrame:CGRectMake(CELL_PADDING, offset, KEY_WIDTH, FONT_SIZE)];
  126. keyLabel.text = [NSString stringWithFormat:@"%@:", text];
  127. keyLabel.font = [UIFont systemFontOfSize:FONT_SIZE];
  128. keyLabel.textColor = [UIColor grayColor];
  129. [self.containerView addSubview:keyLabel];
  130. [self.itemViews addObject:keyLabel];
  131. }
  132. - (CGFloat)addValueLabel:(CGFloat)offset text:(NSString *)text color:(NSString *)colorStr {
  133. UIColor *color = colorStr.length ? [UIColor colorWithHexString:colorStr] : [UIColor grayColor];
  134. if(color == [UIColor clearColor]) {
  135. color = [UIColor grayColor];
  136. }
  137. CGFloat containerWidth = [UIScreen mainScreen].bounds.size.width - CELL_MARGIN - CELL_MARGIN;
  138. CGSize itemSize = [WFCUUtilities getTextDrawingSize:text font:[UIFont systemFontOfSize:FONT_SIZE] constrainedSize:CGSizeMake(containerWidth-CELL_PADDING-CELL_PADDING-VALUE_BOARD_PADDING_LEFT, 50)];
  139. UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(CELL_PADDING+VALUE_BOARD_PADDING_LEFT, offset, containerWidth-CELL_PADDING-CELL_PADDING-VALUE_BOARD_PADDING_LEFT, itemSize.height)];
  140. valueLabel.text = text;
  141. valueLabel.font = [UIFont systemFontOfSize:FONT_SIZE];
  142. valueLabel.numberOfLines = 0;
  143. valueLabel.textColor = color;
  144. [self.containerView addSubview:valueLabel];
  145. [self.itemViews addObject:valueLabel];
  146. return itemSize.height;
  147. }
  148. - (void)removeAllItems {
  149. [self.itemViews enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  150. [obj removeFromSuperview];
  151. }];
  152. [self.itemViews removeAllObjects];
  153. }
  154. - (CGFloat)setLabel:(UILabel *)label widht:(CGFloat)width text:(NSString *)text offset:(CGFloat)offset fontSize:(int)fontSize {
  155. CGSize titleSize = [WFCUUtilities getTextDrawingSize:text font:[UIFont systemFontOfSize:fontSize] constrainedSize:CGSizeMake(width, 50)];
  156. label.text = text;
  157. CGRect frame = label.frame;
  158. frame.origin.y = offset;
  159. frame.size.height = titleSize.height;
  160. label.frame = frame;
  161. label.numberOfLines = 0;
  162. return titleSize.height;
  163. }
  164. -(NSMutableArray<UIView *> *)itemViews {
  165. if(!_itemViews) {
  166. _itemViews = [[NSMutableArray alloc] init];
  167. }
  168. return _itemViews;
  169. }
  170. - (UIView *)containerView {
  171. if(!_containerView) {
  172. _containerView = [[UIView alloc] initWithFrame:CGRectMake(CELL_MARGIN, CELL_MARGIN_TOP_BUTTOM, [UIScreen mainScreen].bounds.size.width - CELL_MARGIN - CELL_MARGIN, 0)];
  173. _containerView.backgroundColor = [UIColor whiteColor];
  174. _containerView.layer.masksToBounds = YES;
  175. _containerView.layer.cornerRadius = 5.f;
  176. UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onDoubleTaped:)];
  177. doubleTapGesture.numberOfTapsRequired = 2;
  178. doubleTapGesture.numberOfTouchesRequired = 1;
  179. [_containerView addGestureRecognizer:doubleTapGesture];
  180. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTaped:)];
  181. [_containerView addGestureRecognizer:tap];
  182. [tap requireGestureRecognizerToFail:doubleTapGesture];
  183. tap.cancelsTouchesInView = NO;
  184. [_containerView setUserInteractionEnabled:YES];
  185. [self.contentView addSubview:_containerView];
  186. }
  187. return _containerView;
  188. }
  189. - (UILabel *)titleLabel {
  190. if(!_titleLabel) {
  191. _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(CELL_PADDING, CELL_PADDING_TOP, [UIScreen mainScreen].bounds.size.width - CELL_MARGIN - CELL_MARGIN - CELL_PADDING - CELL_PADDING, TITLE_FONT_SIZE)];
  192. [_titleLabel setFont:[UIFont systemFontOfSize:TITLE_FONT_SIZE]];
  193. [_titleLabel setTextColor:[UIColor blackColor]];
  194. [self.containerView addSubview:_titleLabel];
  195. }
  196. return _titleLabel;
  197. }
  198. -(UILabel *)descLabel {
  199. if(!_descLabel) {
  200. _descLabel = [[UILabel alloc] initWithFrame:CGRectMake(CELL_PADDING, CELL_PADDING_TOP, [UIScreen mainScreen].bounds.size.width - CELL_MARGIN - CELL_MARGIN - CELL_PADDING - CELL_PADDING, FONT_SIZE)];
  201. [_descLabel setFont:[UIFont systemFontOfSize:FONT_SIZE]];
  202. [_descLabel setTextColor:[UIColor grayColor]];
  203. [self.containerView addSubview:_descLabel];
  204. }
  205. return _descLabel;
  206. }
  207. -(UILabel *)remarkLabel {
  208. if(!_remarkLabel) {
  209. _remarkLabel = [[UILabel alloc] initWithFrame:CGRectMake(CELL_PADDING, CELL_PADDING_TOP, [UIScreen mainScreen].bounds.size.width - CELL_MARGIN - CELL_MARGIN - CELL_PADDING - CELL_PADDING, FONT_SIZE)];
  210. [_remarkLabel setFont:[UIFont systemFontOfSize:FONT_SIZE]];
  211. [_remarkLabel setTextColor:[UIColor grayColor]];
  212. [self.containerView addSubview:_remarkLabel];
  213. }
  214. return _remarkLabel;
  215. }
  216. - (UIView *)exView {
  217. if(!_exView) {
  218. _exView = [[UILabel alloc] initWithFrame:CGRectMake(0, CELL_PADDING_TOP, [UIScreen mainScreen].bounds.size.width - CELL_MARGIN - CELL_MARGIN, EX_FONT_SIZE + CELL_ITEM_PADDING + CELL_PADDING_BUTTOM + EX_LINE_WIDTH)];
  219. _exLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _exView.frame.size.width, 0.5)];
  220. _exLine.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.9];
  221. [_exView addSubview:_exLine];
  222. _exFWView = [[UIImageView alloc] initWithFrame:CGRectMake(_exView.frame.size.width - CELL_PADDING - EX_FW_WIDTH, EX_LINE_WIDTH, EX_FW_WIDTH, EX_FW_WIDTH)];
  223. _exFWView.image = [WFCUImage imageNamed:@"forward_normal"];
  224. [_exView addSubview:_exFWView];
  225. _exPortraitView = [[UIImageView alloc] initWithFrame:CGRectMake(CELL_PADDING, EX_LINE_WIDTH + (EX_FW_WIDTH - EX_FONT_SIZE)/2, EX_FONT_SIZE, EX_FONT_SIZE)];
  226. [_exView addSubview:_exPortraitView];
  227. [self.containerView addSubview:_exView];
  228. }
  229. return _exView;
  230. }
  231. -(UILabel *)exName {
  232. if(!_exName) {
  233. _exName = [[UILabel alloc] initWithFrame:CGRectMake(CELL_PADDING + EX_FW_WIDTH, EX_LINE_WIDTH+CELL_ITEM_PADDING, self.exView.frame.size.width - CELL_PADDING - CELL_PADDING - EX_FW_WIDTH - EX_FW_WIDTH, EX_FONT_SIZE)];
  234. _exName.font = [UIFont systemFontOfSize:EX_FONT_SIZE];
  235. [self.exView addSubview:_exName];
  236. }
  237. return _exName;
  238. }
  239. @end