123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- //
- // DRTimeLineCommentView.m
- // YouHuiProject
- //
- // Created by 小花 on 2018/12/28.
- // Copyright © 2018年 kuxuan. All rights reserved.
- //
- #import "DRTimeLineCommentView.h"
- #import "MLLinkLabel.h"
- #import "UIView+SDAutoLayout.h"
- #import "CCCopyLabel.h"
- @interface DRTimeLineCommentView ()<MLLinkLabelDelegate>
- @property (nonatomic, strong) NSArray *likeItemsArray;
- @property (nonatomic, strong) UIImageView *bgImageView;
- @property (nonatomic, strong) MLLinkLabel *likeLabel;
- @property (nonatomic, strong) CCCopyLabel *contentLabel;
- @property (nonatomic, strong) UIView *likeLableBottomLine;
- @end
- @implementation DRTimeLineCommentView
- - (instancetype)initWithFrame:(CGRect)frame
- {
- if (self = [super initWithFrame:frame]) {
- self.clipsToBounds = YES;
- [self setupViews];
- }
- return self;
- }
- - (void)setupViews
- {
- _bgImageView = [UIImageView new];
- UIImage *bgImage = [UIImage imageNamed:@"LikeCmtBg"];
- bgImage = [bgImage stretchableImageWithLeftCapWidth:bgImage.size.width*0.5 topCapHeight:bgImage.size.height*0.5];
- _bgImageView.image = bgImage;
- _bgImageView.backgroundColor = [UIColor clearColor];
- [self addSubview:_bgImageView];
-
-
- _likeLabel = [[MLLinkLabel alloc] init];
- _likeLabel.font = [UIFont systemFontOfSize:14];
- _likeLabel.numberOfLines = 2;
- _likeLabel.linkTextAttributes = @{NSForegroundColorAttributeName : [UIColor YHColorWithHex:0x576B95]};
- _likeLabel.isAttributedContent = YES;
- [self addSubview:_likeLabel];
-
- _likeLableBottomLine = [UIView new];
- _likeLableBottomLine.backgroundColor = [UIColor YHColorWithHex:0xDDDEDF alpha:0.4];
- [self addSubview:_likeLableBottomLine];
-
- _contentLabel = [[CCCopyLabel alloc] init];
- _contentLabel.textColor = [UIColor YHColorWithHex:0x222222];
- _contentLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:14];
- _contentLabel.numberOfLines = 0;
- _contentLabel.backgroundColor = [UIColor clearColor];
- _contentLabel.longTapBlock = ^{
- [MobClick event:OptimizingCircleCopyBottomContent];
- };
- [self addSubview:_contentLabel];
-
- _bgImageView.sd_layout.spaceToSuperView(UIEdgeInsetsMake(0, 0, 0, 0));
-
- }
- - (void)setLikeItemsArray:(NSArray *)likeItemsArray
- {
- _likeItemsArray = likeItemsArray;
-
- NSTextAttachment *attach = [NSTextAttachment new];
- attach.image = [UIImage imageNamed:@"fenxiang-7"];
- attach.bounds = CGRectMake(0, -2.5, 14, 14);
- NSAttributedString *likeIcon = [NSAttributedString attributedStringWithAttachment:attach];
- NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:@" "];
- [attributedText insertAttributedString:likeIcon atIndex:0];
-
- for (int i = 0; i < likeItemsArray.count; i++) {
- if (i > 0) {
- [attributedText appendAttributedString:[[NSAttributedString alloc] initWithString:@", "]];
- }
- NSString *str = likeItemsArray[i];
- [attributedText appendAttributedString:[self generateAttributedStringWithLikeItemString:str]];
- }
-
-
-
- _likeLabel.attributedText = [attributedText copy];
- }
- - (void)setupWithLikeItemsArray:(NSArray *)likeItemsArray contentText:(NSString *)content
- {
- self.likeItemsArray = likeItemsArray;
-
-
- if (!likeItemsArray.count && content.length==0) {
- self.fixedWidth = @(0); // 如果没有评论或者点赞,设置commentview的固定宽度为0(设置了fixedWith的控件将不再在自动布局过程中调整宽度)
- self.fixedHeight = @(0); // 如果没有评论或者点赞,设置commentview的固定高度为0(设置了fixedHeight的控件将不再在自动布局过程中调整高度)
- return;
- } else {
- self.fixedHeight = nil; // 取消固定宽度约束
- self.fixedWidth = nil; // 取消固定高度约束
- }
-
- CGFloat margin = 10;
-
- UIView *lastTopView = nil;
-
- if (likeItemsArray.count) {
- _likeLabel.sd_resetLayout
- .leftSpaceToView(self, margin)
- .rightSpaceToView(self, margin)
- .topSpaceToView(lastTopView, 13)
- .autoHeightRatio(0);
-
- lastTopView = _likeLabel;
- } else {
- _likeLabel.attributedText = nil;
- _likeLabel.sd_resetLayout
- .heightIs(0);
- }
- [_likeLabel setMaxNumberOfLinesToShow:2];
-
- if (self.likeItemsArray.count) {
- _likeLableBottomLine.sd_resetLayout
- .leftSpaceToView(self, 0)
- .rightSpaceToView(self, 0)
- .heightIs(1)
- .topSpaceToView(lastTopView, 7);
-
- lastTopView = _likeLableBottomLine;
- } else {
- _likeLableBottomLine.sd_resetLayout.heightIs(0);
- }
-
- if (content.length > 0) {
- CGFloat topMargin;
- if (lastTopView) {
- topMargin = 5;
- }else {
- topMargin = 15;
- }
- _contentLabel.sd_resetLayout
- .leftSpaceToView(self, 10)
- .rightSpaceToView(self, 10)
- .topSpaceToView(lastTopView, topMargin)
- .autoHeightRatio(0);
- // [self setLineSpace:2 withText:content inLabel:_contentLabel];
- _contentLabel.text = content;
- lastTopView = _contentLabel;
- }else {
- _contentLabel.text = nil;
- _contentLabel.sd_resetLayout
- .heightIs(0);
- }
-
-
- [self setupAutoHeightWithBottomView:lastTopView bottomMargin:6];
- }
- -(void)setLineSpace:(CGFloat)lineSpace withText:(NSString *)text inLabel:(UILabel *)label{
- if (!text || !label) {
- return;
- }
- NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
- paragraphStyle.lineSpacing = lineSpace; //设置行间距
- paragraphStyle.lineBreakMode = label.lineBreakMode;
- // paragraphStyle.alignment = label.textAlignment;
-
- NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
- [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])];
- label.attributedText = attributedString;
- }
- - (NSMutableAttributedString *)generateAttributedStringWithLikeItemString:(NSString *)string
- {
- NSString *text = string;
- NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:text];
- UIColor *highLightColor = [UIColor YHColorWithHex:0x576B95];
- [attString setAttributes:@{NSForegroundColorAttributeName : highLightColor,NSFontAttributeName:[UIFont boldSystemFontOfSize:14]} range:[text rangeOfString:text]];
-
- return attString;
- }
- #pragma mark - private actions
- #pragma mark - MLLinkLabelDelegate
- - (void)didClickLink:(MLLink *)link linkText:(NSString *)linkText linkLabel:(MLLinkLabel *)linkLabel
- {
-
-
- }
- @end
|