// // KDPMessageTableViewCell.m // KuDianProject // // Created by admin on 2019/7/11. // Copyright © 2019 KDP. All rights reserved. // #import "KDPMessageTableViewCell.h" #import "KDPNoticeModel.h" @implementation KDPMessageTableViewCell { UILabel *_titleLabel; UILabel *_timeLabel; UIView *_grayView; UIImageView *_iconImageView; UILabel *_contentLabel; UIView *_isView; } - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { [self setContentView]; [self setContentViewConstraints]; } return self; } - (void)setContentView{ self.backgroundColor = [UIColor clearColor]; self.contentView.backgroundColor = [UIColor whiteColor]; self.contentView.layer.cornerRadius = 10; self.contentView.layer.masksToBounds = YES; _titleLabel = [[UILabel alloc] init]; _titleLabel.textColor = [UIColor colorWithHex:0x383838]; _titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size: 15]; [self.contentView addSubview:_titleLabel]; _timeLabel = [[UILabel alloc] init]; _timeLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size: 11]; _timeLabel.textColor = [UIColor colorWithHex:0x9B9B9B]; _timeLabel.textAlignment = NSTextAlignmentRight; [self.contentView addSubview:_timeLabel]; _grayView = [[UIView alloc] init]; _grayView.backgroundColor = [UIColor colorWithHex:0xF9F9F9]; _grayView.layer.cornerRadius = 4; _grayView.layer.masksToBounds = YES; [self.contentView addSubview:_grayView]; _isView =[[UIView alloc]init]; _isView.layer.cornerRadius=3; _isView.layer.masksToBounds=YES; _isView.backgroundColor=[UIColor colorWithHex:0xFA2B36]; [self.contentView addSubview:_isView]; _iconImageView = [[UIImageView alloc] init]; [_grayView addSubview:_iconImageView]; _contentLabel = [[UILabel alloc] init]; _contentLabel.numberOfLines = 0; _contentLabel.textColor = [UIColor colorWithHex:0x333333]; _contentLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size: 14]; [_contentLabel sizeToFit]; [_grayView addSubview:_contentLabel]; } - (void)setContentViewConstraints{ [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(UIEdgeInsetsMake(10, 12, 0, 12)); }]; [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.contentView.mas_top).offset(10); make.right.equalTo(self.contentView.mas_right).offset(-20); make.size.equalTo(CGSizeMake(120, 20)); }]; [_isView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self->_timeLabel.mas_right).offset(2); make.width.height.mas_equalTo(6); make.centerY.equalTo(self->_timeLabel); }]; [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.contentView.mas_left).offset(9); make.top.equalTo(self.contentView.mas_top).offset(9); make.height.equalTo(21); make.right.equalTo(self->_timeLabel.mas_left).offset(-10); }]; [_grayView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.contentView.mas_left).offset(10); make.right.equalTo(self.contentView.mas_right).offset(-10); make.top.equalTo(self->_titleLabel.mas_bottom).offset(7); make.bottom.equalTo(self.contentView.mas_bottom).offset(-14); }]; [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.top.bottom.equalTo(self->_grayView); make.width.equalTo(self->_grayView.mas_height); }]; [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self->_iconImageView.mas_right).offset(11); make.top.equalTo(self->_grayView.mas_top).offset(6); make.bottom.equalTo(self->_grayView.mas_bottom).offset(-6); make.right.equalTo(self->_grayView.mas_right).offset(-6); }]; } - (void)configWithViewModel:(id)viewModel indexpath:(NSIndexPath *)indexPath{ KDPNoticeModel *noticeModel = (KDPNoticeModel *)viewModel; _titleLabel.text = noticeModel.title; _timeLabel.text = noticeModel.push_at; [_iconImageView sd_setImageWithURL:[NSURL URLWithString:noticeModel.image_url]placeholderImage:[UIImage imageNamed:placholderImg]]; _contentLabel.text = noticeModel.message; [self setLineSpace:5.0f withText:noticeModel.message inLabel:_contentLabel]; } -(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; } @end