SDTimeLineCellCommentView.m 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //
  2. // SDTimeLineCellCommentView.m
  3. // GSD_WeiXin(wechat)
  4. //
  5. // Created by gsd on 16/2/25.
  6. // Copyright © 2016年 GSD. All rights reserved.
  7. //
  8. /*
  9. *********************************************************************************
  10. *
  11. * GSD_WeiXin
  12. *
  13. * QQ交流群: 459274049
  14. * Email : gsdios@126.com
  15. * GitHub: https://github.com/gsdios/GSD_WeiXin
  16. * 新浪微博:GSD_iOS
  17. *
  18. * 此“高仿微信”用到了很高效方便的自动布局库SDAutoLayout(一行代码搞定自动布局)
  19. * SDAutoLayout地址:https://github.com/gsdios/SDAutoLayout
  20. * SDAutoLayout视频教程:http://www.letv.com/ptv/vplay/24038772.html
  21. * SDAutoLayout用法示例:https://github.com/gsdios/SDAutoLayout/blob/master/README.md
  22. *
  23. *********************************************************************************
  24. */
  25. #import "SDTimeLineCellCommentView.h"
  26. #import "UIView+SDAutoLayout.h"
  27. #import "SDTimeLineCellModel.h"
  28. #import "MLLinkLabel.h"
  29. #import "LEETheme.h"
  30. @interface SDTimeLineCellCommentView () <MLLinkLabelDelegate>
  31. @property (nonatomic, strong) NSArray *likeItemsArray;
  32. @property (nonatomic, strong) NSArray *commentItemsArray;
  33. @property (nonatomic, strong) UIImageView *bgImageView;
  34. @property (nonatomic, strong) MLLinkLabel *likeLabel;
  35. @property (nonatomic, strong) UIView *likeLableBottomLine;
  36. @property (nonatomic, strong) NSMutableArray *commentLabelsArray;
  37. @end
  38. @implementation SDTimeLineCellCommentView
  39. - (instancetype)initWithFrame:(CGRect)frame
  40. {
  41. if (self = [super initWithFrame:frame]) {
  42. [self setupViews];
  43. //设置主题
  44. [self configTheme];
  45. }
  46. return self;
  47. }
  48. - (void)setupViews
  49. {
  50. _bgImageView = [UIImageView new];
  51. UIImage *bgImage = [[[UIImage imageNamed:@"LikeCmtBg"] stretchableImageWithLeftCapWidth:40 topCapHeight:30] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
  52. _bgImageView.image = bgImage;
  53. _bgImageView.backgroundColor = SLColor(@"#F5F8F7");
  54. [self addSubview:_bgImageView];
  55. _likeLabel = [MLLinkLabel new];
  56. _likeLabel.font = [UIFont systemFontOfSize:12];
  57. _likeLabel.linkTextAttributes = @{NSForegroundColorAttributeName : SLColor(@"#384962")};
  58. _likeLabel.isAttributedContent = YES;
  59. _likeLabel.backgroundColor = SLColor(@"#F5F8F7");
  60. [self addSubview:_likeLabel];
  61. _likeLableBottomLine = [UIView new];
  62. _likeLableBottomLine.backgroundColor = [UIColor whiteColor];
  63. [self addSubview:_likeLableBottomLine];
  64. _bgImageView.sd_layout.spaceToSuperView(UIEdgeInsetsMake(0, 0, 0, 0));
  65. }
  66. - (void)configTheme{
  67. self.lee_theme
  68. .LeeAddBackgroundColor(DAY , SLColor(@"#F5F8F7"))
  69. .LeeAddBackgroundColor(NIGHT , [UIColor blackColor]);
  70. _bgImageView.lee_theme
  71. .LeeAddTintColor(DAY , SDColor(230, 230, 230, 1.0f))
  72. .LeeAddTintColor(NIGHT , SDColor(30, 30, 30, 1.0f));
  73. _likeLabel.lee_theme
  74. .LeeAddTextColor(DAY , SLColor(@"#384962"))
  75. .LeeAddTextColor(NIGHT , [UIColor grayColor]);
  76. _likeLableBottomLine.lee_theme
  77. .LeeAddBackgroundColor(DAY , SDColor(210, 210, 210, 1.0f))
  78. .LeeAddBackgroundColor(NIGHT , SDColor(60, 60, 60, 1.0f));
  79. }
  80. - (void)setCommentItemsArray:(NSArray *)commentItemsArray
  81. {
  82. _commentItemsArray = commentItemsArray;
  83. long originalLabelsCount = self.commentLabelsArray.count;
  84. long needsToAddCount = commentItemsArray.count > originalLabelsCount ? (commentItemsArray.count - originalLabelsCount) : 0;
  85. for (int i = 0; i < needsToAddCount; i++) {
  86. MLLinkLabel *label = [MLLinkLabel new];
  87. UIColor *highLightColor = SLColor(@"#48484A");
  88. label.linkTextAttributes = @{NSForegroundColorAttributeName : highLightColor, NSFontAttributeName: [UIFont boldSystemFontOfSize:12]};
  89. label.font = [UIFont systemFontOfSize:12];
  90. label.textColor = SLColor(@"#48484A");
  91. label.delegate = self;
  92. [self addSubview:label];
  93. [self.commentLabelsArray addObject:label];
  94. }
  95. for (int i = 0; i < commentItemsArray.count; i++) {
  96. SDTimeLineCellCommentItemModel *model = commentItemsArray[i];
  97. MLLinkLabel *label = self.commentLabelsArray[i];
  98. if (!model.attributedContent) {
  99. model.attributedContent = [self generateAttributedStringWithCommentItemModel:model];
  100. }
  101. label.attributedText = model.attributedContent;
  102. }
  103. }
  104. - (void)setLikeItemsArray:(NSArray *)likeItemsArray
  105. {
  106. _likeItemsArray = likeItemsArray;
  107. NSTextAttachment *attach = [NSTextAttachment new];
  108. attach.image = [UIImage imageNamed:@"icon_heart"];
  109. attach.bounds = CGRectMake(0, -3, 16, 16);
  110. NSAttributedString *likeIcon = [NSAttributedString attributedStringWithAttachment:attach];
  111. NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:likeIcon];
  112. for (int i = 0; i < likeItemsArray.count; i++) {
  113. SDTimeLineCellLikeItemModel *model = likeItemsArray[i];
  114. if (i == 0) {
  115. [attributedText appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
  116. }
  117. if (i > 0) {
  118. [attributedText appendAttributedString:[[NSAttributedString alloc] initWithString:@","]];
  119. }
  120. if (!model.attributedContent) {
  121. model.attributedContent = [self generateAttributedStringWithLikeItemModel:model];
  122. }
  123. [attributedText appendAttributedString:model.attributedContent];
  124. }
  125. _likeLabel.attributedText = [attributedText copy];
  126. }
  127. - (NSMutableArray *)commentLabelsArray
  128. {
  129. if (!_commentLabelsArray) {
  130. _commentLabelsArray = [NSMutableArray new];
  131. }
  132. return _commentLabelsArray;
  133. }
  134. - (void)setupWithLikeItemsArray:(NSArray *)likeItemsArray commentItemsArray:(NSArray *)commentItemsArray
  135. {
  136. self.likeItemsArray = likeItemsArray;
  137. self.commentItemsArray = commentItemsArray;
  138. if (self.commentLabelsArray.count) {
  139. [self.commentLabelsArray enumerateObjectsUsingBlock:^(UILabel *label, NSUInteger idx, BOOL *stop) {
  140. [label sd_clearAutoLayoutSettings];
  141. label.hidden = YES; //重用时先隐藏所以评论label,然后根据评论个数显示label
  142. }];
  143. }
  144. if (!commentItemsArray.count && !likeItemsArray.count) {
  145. self.fixedWidth = @(0); // 如果没有评论或者点赞,设置commentview的固定宽度为0(设置了fixedWith的控件将不再在自动布局过程中调整宽度)
  146. self.fixedHeight = @(0); // 如果没有评论或者点赞,设置commentview的固定高度为0(设置了fixedHeight的控件将不再在自动布局过程中调整高度)
  147. return;
  148. } else {
  149. self.fixedHeight = nil; // 取消固定宽度约束
  150. self.fixedWidth = nil; // 取消固定高度约束
  151. }
  152. CGFloat margin = 0;
  153. UIView *lastTopView = nil;
  154. if (likeItemsArray.count) {
  155. _likeLabel.sd_resetLayout
  156. .leftSpaceToView(self, margin)
  157. .rightSpaceToView(self, margin)
  158. .topSpaceToView(lastTopView, 3)
  159. .autoHeightRatio(0);
  160. lastTopView = _likeLabel;
  161. } else {
  162. _likeLabel.attributedText = nil;
  163. _likeLabel.sd_resetLayout
  164. .heightIs(0);
  165. }
  166. if (self.commentItemsArray.count && self.likeItemsArray.count) {
  167. _likeLableBottomLine.sd_resetLayout
  168. .leftSpaceToView(self, 0)
  169. .rightSpaceToView(self, 0)
  170. .heightIs(5)
  171. .topSpaceToView(_likeLabel, 3);
  172. lastTopView = _likeLableBottomLine;
  173. } else {
  174. _likeLableBottomLine.sd_resetLayout.heightIs(0);
  175. }
  176. for (int i = 0; i < self.commentItemsArray.count; i++) {
  177. UILabel *label = (UILabel *)self.commentLabelsArray[i];
  178. label.hidden = NO;
  179. CGFloat topMargin = (i == 0 && likeItemsArray.count == 0) ? 10 : 5;
  180. label.sd_layout
  181. .leftSpaceToView(self, 8)
  182. .rightSpaceToView(self, 5)
  183. .topSpaceToView(lastTopView, topMargin)
  184. .autoHeightRatio(0);
  185. label.isAttributedContent = YES;
  186. lastTopView = label;
  187. }
  188. [self setupAutoHeightWithBottomView:lastTopView bottomMargin:6];
  189. }
  190. - (void)setFrame:(CGRect)frame
  191. {
  192. [super setFrame:frame];
  193. }
  194. #pragma mark - private actions
  195. - (NSMutableAttributedString *)generateAttributedStringWithCommentItemModel:(SDTimeLineCellCommentItemModel *)model
  196. {
  197. NSString *text = model.firstUserName;
  198. if (model.secondUserName.length) {
  199. text = [text stringByAppendingString:[NSString stringWithFormat:@" 回复 %@", model.secondUserName]];
  200. }
  201. text = [text stringByAppendingString:[NSString stringWithFormat:@":%@", model.commentString]];
  202. NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:text];
  203. [attString setAttributes:@{NSLinkAttributeName : model.firstUserId} range:[text rangeOfString:model.firstUserName]];
  204. if (model.secondUserName) {
  205. [attString setAttributes:@{NSLinkAttributeName : model.secondUserId} range:[text rangeOfString:model.secondUserName]];
  206. }
  207. return attString;
  208. }
  209. - (NSMutableAttributedString *)generateAttributedStringWithLikeItemModel:(SDTimeLineCellLikeItemModel *)model
  210. {
  211. NSString *text = model.userName;
  212. NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:text];
  213. UIColor *highLightColor = SLColor(@"#384962");
  214. [attString setAttributes:@{NSForegroundColorAttributeName : highLightColor, NSLinkAttributeName : model.userId} range:[text rangeOfString:model.userName]];
  215. return attString;
  216. }
  217. #pragma mark - MLLinkLabelDelegate
  218. - (void)didClickLink:(MLLink *)link linkText:(NSString *)linkText linkLabel:(MLLinkLabel *)linkLabel
  219. {
  220. NSLog(@"%@", link.linkValue);
  221. }
  222. @end