// // 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 () @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