《省钱达人》与《猎豆优选》UI相同版。域名tbk

DRTimeLineCommentView.m 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // DRTimeLineCommentView.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/12/28.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "DRTimeLineCommentView.h"
  9. #import "MLLinkLabel.h"
  10. #import "UIView+SDAutoLayout.h"
  11. #import "CCCopyLabel.h"
  12. @interface DRTimeLineCommentView ()<MLLinkLabelDelegate>
  13. @property (nonatomic, strong) NSArray *likeItemsArray;
  14. @property (nonatomic, strong) UIImageView *bgImageView;
  15. @property (nonatomic, strong) MLLinkLabel *likeLabel;
  16. @property (nonatomic, strong) CCCopyLabel *contentLabel;
  17. @property (nonatomic, strong) UIView *likeLableBottomLine;
  18. @end
  19. @implementation DRTimeLineCommentView
  20. - (instancetype)initWithFrame:(CGRect)frame
  21. {
  22. if (self = [super initWithFrame:frame]) {
  23. self.clipsToBounds = YES;
  24. [self setupViews];
  25. }
  26. return self;
  27. }
  28. - (void)setupViews
  29. {
  30. _bgImageView = [UIImageView new];
  31. UIImage *bgImage = [UIImage imageNamed:@"LikeCmtBg"];
  32. bgImage = [bgImage stretchableImageWithLeftCapWidth:bgImage.size.width*0.5 topCapHeight:bgImage.size.height*0.5];
  33. _bgImageView.image = bgImage;
  34. _bgImageView.backgroundColor = [UIColor clearColor];
  35. [self addSubview:_bgImageView];
  36. _likeLabel = [[MLLinkLabel alloc] init];
  37. _likeLabel.font = [UIFont systemFontOfSize:14];
  38. _likeLabel.numberOfLines = 2;
  39. _likeLabel.linkTextAttributes = @{NSForegroundColorAttributeName : [UIColor YHColorWithHex:0x576B95]};
  40. _likeLabel.isAttributedContent = YES;
  41. [self addSubview:_likeLabel];
  42. _likeLableBottomLine = [UIView new];
  43. _likeLableBottomLine.backgroundColor = [UIColor YHColorWithHex:0xDDDEDF alpha:0.4];
  44. [self addSubview:_likeLableBottomLine];
  45. _contentLabel = [[CCCopyLabel alloc] init];
  46. _contentLabel.textColor = [UIColor YHColorWithHex:0x222222];
  47. _contentLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:14];
  48. _contentLabel.numberOfLines = 0;
  49. _contentLabel.backgroundColor = [UIColor clearColor];
  50. _contentLabel.longTapBlock = ^{
  51. [MobClick event:OptimizingCircleCopyBottomContent];
  52. };
  53. [self addSubview:_contentLabel];
  54. _bgImageView.sd_layout.spaceToSuperView(UIEdgeInsetsMake(0, 0, 0, 0));
  55. }
  56. - (void)setLikeItemsArray:(NSArray *)likeItemsArray
  57. {
  58. _likeItemsArray = likeItemsArray;
  59. NSTextAttachment *attach = [NSTextAttachment new];
  60. attach.image = [UIImage imageNamed:@"fenxiang-7"];
  61. attach.bounds = CGRectMake(0, -2.5, 14, 14);
  62. NSAttributedString *likeIcon = [NSAttributedString attributedStringWithAttachment:attach];
  63. NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:@" "];
  64. [attributedText insertAttributedString:likeIcon atIndex:0];
  65. for (int i = 0; i < likeItemsArray.count; i++) {
  66. if (i > 0) {
  67. [attributedText appendAttributedString:[[NSAttributedString alloc] initWithString:@", "]];
  68. }
  69. NSString *str = likeItemsArray[i];
  70. [attributedText appendAttributedString:[self generateAttributedStringWithLikeItemString:str]];
  71. }
  72. _likeLabel.attributedText = [attributedText copy];
  73. }
  74. - (void)setupWithLikeItemsArray:(NSArray *)likeItemsArray contentText:(NSString *)content
  75. {
  76. self.likeItemsArray = likeItemsArray;
  77. if (!likeItemsArray.count && content.length==0) {
  78. self.fixedWidth = @(0); // 如果没有评论或者点赞,设置commentview的固定宽度为0(设置了fixedWith的控件将不再在自动布局过程中调整宽度)
  79. self.fixedHeight = @(0); // 如果没有评论或者点赞,设置commentview的固定高度为0(设置了fixedHeight的控件将不再在自动布局过程中调整高度)
  80. return;
  81. } else {
  82. self.fixedHeight = nil; // 取消固定宽度约束
  83. self.fixedWidth = nil; // 取消固定高度约束
  84. }
  85. CGFloat margin = 10;
  86. UIView *lastTopView = nil;
  87. if (likeItemsArray.count) {
  88. _likeLabel.sd_resetLayout
  89. .leftSpaceToView(self, margin)
  90. .rightSpaceToView(self, margin)
  91. .topSpaceToView(lastTopView, 13)
  92. .autoHeightRatio(0);
  93. lastTopView = _likeLabel;
  94. } else {
  95. _likeLabel.attributedText = nil;
  96. _likeLabel.sd_resetLayout
  97. .heightIs(0);
  98. }
  99. [_likeLabel setMaxNumberOfLinesToShow:2];
  100. if (self.likeItemsArray.count) {
  101. _likeLableBottomLine.sd_resetLayout
  102. .leftSpaceToView(self, 0)
  103. .rightSpaceToView(self, 0)
  104. .heightIs(1)
  105. .topSpaceToView(lastTopView, 7);
  106. lastTopView = _likeLableBottomLine;
  107. } else {
  108. _likeLableBottomLine.sd_resetLayout.heightIs(0);
  109. }
  110. if (content.length > 0) {
  111. CGFloat topMargin;
  112. if (lastTopView) {
  113. topMargin = 5;
  114. }else {
  115. topMargin = 15;
  116. }
  117. _contentLabel.sd_resetLayout
  118. .leftSpaceToView(self, 10)
  119. .rightSpaceToView(self, 10)
  120. .topSpaceToView(lastTopView, topMargin)
  121. .autoHeightRatio(0);
  122. // [self setLineSpace:2 withText:content inLabel:_contentLabel];
  123. _contentLabel.text = content;
  124. lastTopView = _contentLabel;
  125. }else {
  126. _contentLabel.text = nil;
  127. _contentLabel.sd_resetLayout
  128. .heightIs(0);
  129. }
  130. [self setupAutoHeightWithBottomView:lastTopView bottomMargin:6];
  131. }
  132. -(void)setLineSpace:(CGFloat)lineSpace withText:(NSString *)text inLabel:(UILabel *)label{
  133. if (!text || !label) {
  134. return;
  135. }
  136. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  137. paragraphStyle.lineSpacing = lineSpace; //设置行间距
  138. paragraphStyle.lineBreakMode = label.lineBreakMode;
  139. // paragraphStyle.alignment = label.textAlignment;
  140. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
  141. [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])];
  142. label.attributedText = attributedString;
  143. }
  144. - (NSMutableAttributedString *)generateAttributedStringWithLikeItemString:(NSString *)string
  145. {
  146. NSString *text = string;
  147. NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:text];
  148. UIColor *highLightColor = [UIColor YHColorWithHex:0x576B95];
  149. [attString setAttributes:@{NSForegroundColorAttributeName : highLightColor,NSFontAttributeName:[UIFont boldSystemFontOfSize:14]} range:[text rangeOfString:text]];
  150. return attString;
  151. }
  152. #pragma mark - private actions
  153. #pragma mark - MLLinkLabelDelegate
  154. - (void)didClickLink:(MLLink *)link linkText:(NSString *)linkText linkLabel:(MLLinkLabel *)linkLabel
  155. {
  156. }
  157. @end