// // KXCustomServiceTableViewCell.m // CAISHEN // // Created by xiaoxi on 2017/12/6. // Copyright © 2017年 kuxuan. All rights reserved. // #import "KXCustomServiceTableViewCell.h" @implementation KXCustomServiceTableViewCell - (void)awakeFromNib { [super awakeFromNib]; // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { self.selectionStyle = UITableViewCellSelectionStyleNone; self.backgroundColor = [UIColor KXColorWithHex:0xf8f8f8]; [self setupUI]; } return self; } - (void)setupUI { self.line = [[UILabel alloc] initWithFrame:CGRectZero]; self.line.backgroundColor = [UIColor KXColorWithHex:0xf8f8f8]; [self.contentView addSubview:self.line]; self.timeLabel = [[UILabel alloc] initWithFrame:CGRectZero]; self.timeLabel.backgroundColor = [UIColor KXColorWithHex:0xf8f8f8]; self.timeLabel.font = FONT_SYS(12*SCREEN_MUTI); self.timeLabel.textColor = [UIColor KXColorWithHex:0x727171]; self.timeLabel.textAlignment = NSTextAlignmentCenter; [self.contentView addSubview:self.timeLabel]; self.iconView = [[UIImageView alloc] initWithFrame:CGRectZero]; self.iconView.backgroundColor = [UIColor KXColorWithHex:0xf8f8f8]; [self.contentView addSubview:self.iconView]; self.bgView = [[UIImageView alloc] initWithFrame:CGRectZero]; self.bgView.backgroundColor = [UIColor whiteColor]; [self.contentView addSubview:self.bgView]; self.contentLabel = [[UILabel alloc] initWithFrame:CGRectZero]; self.contentLabel.backgroundColor = [UIColor whiteColor]; self.contentLabel.font = FONT_SYS(14*SCREEN_MUTI); self.contentLabel.textColor = [UIColor titleColor]; self.contentLabel.numberOfLines = 0; [self.contentView addSubview:self.contentLabel]; [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self.contentView); make.top.equalTo(self.contentView); make.width.equalTo(@200); }]; [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.contentView).offset(18+44*SCREEN_MUTI+10); make.top.equalTo(self.timeLabel.mas_bottom); }]; [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.contentView).offset(18); make.centerY.equalTo(self.bgView); make.size.mas_equalTo(CGSizeMake(44*SCREEN_MUTI, 44*SCREEN_MUTI)); }]; [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(self.bgView); }]; [self.line mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.contentView); make.top.equalTo(self.bgView.mas_bottom); make.right.equalTo(self.contentView); make.height.equalTo(@14); }]; } - (void)refreshCell:(KXChatModel *)model { CGRect msgRect = [model.msg boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-(18+44+52)*SCREEN_MUTI, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:FONT_SYS(14*SCREEN_MUTI)} context:nil]; CGFloat msgWidth = ceil(msgRect.size.width); CGFloat msgHeight = ceil(msgRect.size.height); if (msgHeight + 20 < 44) { msgHeight = 24; } UIImage *iconImage = nil; UIImage *bgImage = nil; if (model.isShowTime) { self.timeLabel.text = model.time; } else { self.timeLabel.text = @""; [self.timeLabel mas_updateConstraints:^(MASConstraintMaker *make) { make.height.equalTo(@0.5); }]; } if (model.isLeft) { [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(msgWidth+20); make.height.mas_equalTo(msgHeight+20); }]; iconImage = [UIImage imageNamed:@"mine_borrow"]; bgImage = [UIImage imageNamed:@""]; self.iconView.image = [self circleImage:iconImage]; } else { [self.iconView mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.contentView).offset(-18); make.centerY.equalTo(self.bgView); make.size.mas_equalTo(CGSizeMake(44*SCREEN_MUTI, 44*SCREEN_MUTI)); }]; [self.bgView mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.contentView).offset(-18-44*SCREEN_MUTI-10); make.top.equalTo(self.timeLabel.mas_bottom); make.width.mas_equalTo(msgWidth+20); make.height.mas_equalTo(msgHeight+20); }]; iconImage = [UIImage imageNamed:@"default_custom"]; bgImage = [UIImage imageNamed:@""]; self.iconView.image = iconImage; } self.bgView.image = bgImage; [self.contentLabel mas_updateConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(msgWidth); make.height.mas_equalTo(msgHeight); }]; self.contentLabel.text = model.msg; } - (UIImage *)circleImage:(UIImage *)image { // 开始图形上下文 UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0); // 获得图形上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 设置一个范围 CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height); // 根据一个rect创建一个椭圆 CGContextAddEllipseInRect(ctx, rect); // 裁剪 CGContextClip(ctx); // 将原照片画到图形上下文 [image drawInRect:rect]; // 从上下文上获取剪裁后的照片 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); // 关闭上下文 UIGraphicsEndImageContext(); return newImage; } @end