YH_BarrageMsgCell.m 7.18 KB
//
//  YH_BarrageMsgCell.m
//  Yoho
//
//  Created by Arthur on 16/6/14.
//  Copyright © 2016年 YOHO. All rights reserved.
//

#import "YH_BarrageMsgCell.h"
#import "YHAssistant.h"
#import "UIImageView+WebCache.h"
#define kMsgCellDefaultHeight 60

#define kContentMarginTop 6
#define kContentMarginButtom 6
#define kMarginLeft 15
#define kMarginRight 15
#define kMarginTop 9
#define kMarginBottom 9
#define kPandingV    5
#define kPandingH    10

#define kUserAvatarWidth 35
#define kUserNameLableMaxWidth (150 * kScreenPointScale)

#define kFontUserName [UIFont fontOfLightWithSize:11]
#define kFontUserMsg [UIFont fontOfSCWithSize:13]


@interface YH_BarrageMsgCell()
@property (strong, nonatomic)  UIImageView *avatarImageView;
@property (strong, nonatomic)  UILabel *nameLabel;
@property (strong, nonatomic)  UILabel *msgLabel;
@property (strong, nonatomic)  UIView *bgview;
@property (strong, nonatomic) UIColor *randomColor;
@property (strong, nonatomic) NSArray *colorArray;
@end

@implementation YH_BarrageMsgCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
        [self addSubview:self.bgview];
        [self setBackgroundColor:[UIColor clearColor]];
        [self.bgview addSubview:self.avatarImageView];
        [self.bgview addSubview:self.nameLabel];
        [self.bgview addSubview:self.msgLabel];
    }
    
    return self;
}

- (void)reLayoutCellWithWidth:(CGFloat)width msg:(YH_BarrageMsgModel *)msg {
    CGFloat cellHeight = [YH_BarrageMsgCell cellHeightWithMessage:msg];
    CGFloat nameHeight = [msg.userName boundingRectWithSize:CGSizeMake(kUserNameLableMaxWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : kFontUserName} context:nil].size.height;
    CGFloat msgHeight = [msg.message boundingRectWithSize:CGSizeMake(kUserNameLableMaxWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : kFontUserMsg} context:nil].size.height;
    
    [self.bgview setFrame:CGRectMake(kMarginLeft, kContentMarginTop, width - kMarginLeft - kMarginRight, cellHeight - kContentMarginTop-kContentMarginButtom)];
    [self.avatarImageView setFrame:CGRectMake(-1, 0, kUserAvatarWidth, CGRectGetHeight(self.bgview.frame))];
    [self.nameLabel setFrame:CGRectMake(CGRectGetWidth(self.avatarImageView.frame)+kPandingH, kMarginTop, CGRectGetWidth(self.bgview.frame)-kUserAvatarWidth-2*kPandingH, nameHeight)];
    [self.msgLabel setFrame:CGRectMake(CGRectGetWidth(self.avatarImageView.frame)+kPandingH, CGRectGetMaxY(self.nameLabel.frame)+kPandingV, CGRectGetWidth(self.bgview.frame)-kUserAvatarWidth-2*kPandingH, msgHeight)];
    
}

- (CGSize)sizeThatFits:(CGSize)size
{
    CGFloat totalHeight = 0;
    totalHeight += [self.nameLabel sizeThatFits:size].height;
    totalHeight += [self.msgLabel sizeThatFits:size].height;
    totalHeight += 33; // margins
    
    return CGSizeMake(size.width, totalHeight);

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (UIColor *)randomColor {
    
    if (_randomColor == nil) {
//        CGFloat hue = ( arc4random() % 256 / 256.0 );  //  0.0 to 1.0
//        CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from white
//        CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from black
//         _randomColor = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
        NSString *colorHexStr = [self.colorArray objectAtIndex:arc4random() % [self.colorArray count]];
        _randomColor = [UIColor colorWithHexString:colorHexStr];

    }
    
    return _randomColor;
}


- (void)updateCellWithMessage:(YH_BarrageMsgModel *)msgModel cellWidth:(CGFloat)width
{
    __weak typeof (self) weakSelf = self;
    [self reLayoutCellWithWidth:width msg:msgModel];
    
    [self.avatarImageView sd_setImageWithURL:msgModel.avatar placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

        if (image) {
            [weakSelf.avatarImageView setBackgroundColor:[UIColor whiteColor]];
        }
        
            UIBezierPath *maskPath;
            maskPath = [UIBezierPath bezierPathWithRoundedRect:weakSelf.avatarImageView.bounds
                                             byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerTopLeft)
                                                   cornerRadii:CGSizeMake(6.0, 6.0)];
            CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
            maskLayer.frame = weakSelf.bgview.bounds;
            maskLayer.path = maskPath.CGPath;
            weakSelf.avatarImageView.layer.mask = maskLayer;

    }];
    
    [self.nameLabel setText:[NSString stringWithFormat:@"@%@", msgModel.userName]];
    [self.msgLabel setText:msgModel.message];
}


+ (CGFloat)cellHeightWithMessage:(YH_BarrageMsgModel *)msgModel
{
    CGFloat cellHeight = kContentMarginTop + kMarginTop + kMarginBottom + kPandingV + kContentMarginButtom;
    
     cellHeight += [msgModel.userName boundingRectWithSize:CGSizeMake(kUserNameLableMaxWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : kFontUserName} context:nil].size.height;
    
     cellHeight += [msgModel.message boundingRectWithSize:CGSizeMake(kUserNameLableMaxWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : kFontUserMsg} context:nil].size.height;
    
    return cellHeight;
    
}

#pragma mark - getters and setters
- (UIView *)bgview
{
    if (_bgview == nil) {
        _bgview = [[UIView alloc]initWithFrame:CGRectZero];
        [_bgview setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.4]];
        [_bgview.layer setCornerRadius:6.0f];
        
    }
    
    return _bgview;
}


- (UIImageView *)avatarImageView
{
    if (_avatarImageView == nil) {
        _avatarImageView = [[UIImageView alloc]initWithFrame:CGRectZero];
        [_avatarImageView setBackgroundColor:[self randomColor]];
        [_avatarImageView setContentMode:UIViewContentModeScaleAspectFill];
    }
    
    return _avatarImageView;
}

- (UILabel *)nameLabel
{
    if (_nameLabel == nil) {
        _nameLabel = [[UILabel alloc]init];
        [_nameLabel setTextColor:[UIColor colorWithHexString:@"#B0B0B0"]];
        [_nameLabel setTextAlignment:NSTextAlignmentLeft];
        [_nameLabel setFont:kFontUserName];
        [_nameLabel setBackgroundColor:[UIColor clearColor]];
    }
    
    return _nameLabel;
}

- (UILabel *)msgLabel
{
    if (_msgLabel == nil) {
        _msgLabel = [[UILabel alloc]init];
        [_msgLabel setNumberOfLines:0];
        [_msgLabel setTextColor:[UIColor whiteColor]];
        [_msgLabel setTextAlignment:NSTextAlignmentLeft];
        [_msgLabel setFont:kFontUserMsg];
        [_msgLabel setBackgroundColor:[UIColor clearColor]];
    }
    
    return _msgLabel;
}

- (NSArray *)colorArray {
    if (_colorArray == nil) {
        _colorArray = @[@"c0ec94", @"ef7aa3", @"65ebe1", @"5ec9ed", @"fac176"];
    }
    
    return _colorArray;
}
@end