IVTChatRoomCell.m 9.82 KB
//
//  IVTChatRoomCell.m
//  ChinaFM
//
//  Created by XRG on 16/9/26.
//  Copyright © 2016年 XRG. All rights reserved.
//

#import "IVTChatRoomCell.h"
//#import "UIDefine.h"
//#import "AccountManager.h"
#import "UIColor+IVTMeColor.h"


@interface IVTChatRoomCell ()

@property (weak, nonatomic) IBOutlet UILabel *chatContentLabel;

@property (weak, nonatomic) IBOutlet UIImageView *redPacketImageView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *labelToRedPacketConstraint;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *labelToBottomConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *chatLeftConstraint;

@property (strong, nonatomic) UILabel *levelLabel;
@property (strong, nonatomic) UIImageView *levelImgView;

@end

@implementation IVTChatRoomCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.backgroundColor = [UIColor clearColor];
    self.redPacketImageView.userInteractionEnabled = YES;
    self.redPacketImageView.hidden = YES;
    self.labelToBottomConstraint.priority = 900;
    self.labelToRedPacketConstraint.priority = 700;
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectZero];
    [self addSubview:imgView];
    
    self.levelImgView = imgView;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:8];
    [self.levelImgView addSubview:label];

    self.levelLabel = label;

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeGrabView)];
    [self.redPacketImageView addGestureRecognizer:tap];
}

- (void)setEntity:(NSDictionary *)entity{
    _entity = entity;
    
    self.chatLeftConstraint.constant = 0;
    self.levelImgView.hidden = YES;

    if ([entity[@"msgType"] integerValue] == kSystemMessageType) {
        self.chatContentLabel.attributedText = [self getAttributedStringWithNameColorString:@"#fee4bf" chatLabelColorString:@"#fe8aff" isShowSex:NO];
    }
    else if ([entity[@"msgType"] integerValue] == kNormalRoomChatMessageType){
        self.chatContentLabel.attributedText = [self getAttributedStringWithNameColorString:@"#fee4bf" chatLabelColorString:@"#FFFFFF" isShowSex:YES];
    }
    else if ([_entity[@"msgType"] integerValue]== kDanMuRoomChatMessageType) {
        self.chatContentLabel.attributedText = [self getAttributedStringWithNameColorString:@"#fee4bf" chatLabelColorString:@"#FF0000" isShowSex:YES];
    }
    else if ([_entity[@"msgType"] integerValue] == kPrivateRedPacketType){
        self.chatContentLabel.attributedText = [self getAttributedStringWithNameColorString:@"#fee4bf" chatLabelColorString:@"#fee4bf" isShowSex:YES];
    }
    else if ([_entity[@"msgType"] integerValue] == kBigGiftType){
        self.chatContentLabel.attributedText = [self getAttributedStringWithNameColorString:@"#fee4bf" chatLabelColorString:@"#FF00FF" isShowSex:YES];
    }
    else if ([_entity[@"msgType"] integerValue] == kSmallGiftType){
        self.chatContentLabel.attributedText = [self getAttributedStringWithNameColorString:@"#fee4bf" chatLabelColorString:@"#FFC0CB" isShowSex:YES];
    }
    else if ([_entity[@"msgType"] integerValue] == kSetManagerType){
        self.chatContentLabel.attributedText = [self getAttributedStringWithNameColorString:@"#fee4bf" chatLabelColorString:@"#fe8aff" isShowSex:NO];
    }
    else if([_entity[@"msgType"] integerValue] == kCancelManagerType){
        self.chatContentLabel.attributedText = [self getAttributedStringWithNameColorString:@"#fee4bf" chatLabelColorString:@"#fe8aff" isShowSex:NO];
    }
    else if ([_entity[@"msgType"] integerValue] == kCancelBanSendMessageType){
        self.chatContentLabel.attributedText = [self getAttributedStringWithNameColorString:@"#fee4bf" chatLabelColorString:@"#fe8aff" isShowSex:NO];
    }
    else if ([_entity[@"msgType"] integerValue] == kSetBanSendMessageType){
        self.chatContentLabel.attributedText = [self getAttributedStringWithNameColorString:@"#fee4bf" chatLabelColorString:@"#fe8aff" isShowSex:NO];
    }
    else if ([_entity[@"msgType"] integerValue] == kQuitRoomType) {
        self.chatContentLabel.attributedText = [self getAttributedStringWithNameColorString:@"#fee4bf" chatLabelColorString:@"#FFFFFF" isShowSex:NO];
    }
    
    if ([_entity[@"msgType"] integerValue] == kGroupRedPacketType) {
        self.chatContentLabel.attributedText = [self getAttributedStringWithNameColorString:@"#fee4bf" chatLabelColorString:@"#fe8aff" isShowSex:YES];
        self.redPacketImageView.hidden = NO;
        self.labelToBottomConstraint.priority = 700;
        self.labelToRedPacketConstraint.priority = 900;
    }else{
        self.redPacketImageView.hidden = YES;
        self.labelToBottomConstraint.priority = 900;
        self.labelToRedPacketConstraint.priority = 700;
    }
}

- (NSAttributedString *)getAttributedStringWithNameColorString:(NSString *)nameColorString chatLabelColorString:(NSString *)chatTextColorString isShowSex:(BOOL)isShowSex{

    NSString *userName;
    NSString *chatString = [_entity valueForKey:@"msgBody"];

    if ([_entity[@"msgType"] integerValue] == kSystemMessageType) {
        userName = [_entity valueForKeyPath:@"userInfo.userName"];
        chatString = [_entity valueForKey:@"msgBody"];
    } else if ([_entity[@"msgType"] integerValue] == kSetManagerType || [_entity[@"msgType"] integerValue] == kCancelManagerType || [_entity[@"msgType"] integerValue] == kSetBanSendMessageType || [_entity[@"msgType"] integerValue] == kCancelBanSendMessageType) {
        userName = @"系统消息";
        chatString = [[[_entity valueForKeyPath:@"userInfo.userName"] base64DecodedToString] stringByAppendingFormat:@" %@", chatString];
    }else{
        userName = [_entity valueForKeyPath:@"userInfo.userName"];

        NSData *decodeData = [[NSData alloc] initWithBase64EncodedString:userName options:0];
        userName = [[NSString alloc]initWithData:decodeData encoding:NSUTF8StringEncoding];
        
                    
        chatString = [_entity valueForKey:@"msgBody"];
    }
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@:%@",userName,chatString]];

    if (isShowSex) {
        self.chatLeftConstraint.constant = 28+2;
        
        self.levelImgView.frame = CGRectMake(0, 2, 28, 14);
        self.levelImgView.image = [self showLevel:[_entity valueForKeyPath:@"userInfo.level"]].image;

        self.levelLabel.text = [_entity valueForKeyPath:@"userInfo.level"];
        [self.levelLabel sizeToFit];

        self.levelLabel.center = CGPointMake(20, 7);

        self.levelImgView.hidden = NO;
    }
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [paragraphStyle setLineSpacing:5];
    
    NSRange nameRang = [attributedString.string rangeOfString:[NSString stringWithFormat:@"%@:",userName]];
    NSRange chatTextRang = [attributedString.string rangeOfString:[NSString stringWithFormat:@"%@",chatString]];
    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowColor = UIColorFromRGBAlphaHex(0x000000, 0.8);
    shadow.shadowOffset = CGSizeMake(0.5, 0.5);

    NSDictionary *attributesForName = @{NSParagraphStyleAttributeName:paragraphStyle,NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:[UIColor YXColorWithHexCode:nameColorString],NSShadowAttributeName:shadow};
    
    shadow = [[NSShadow alloc] init];
    shadow.shadowColor = UIColorFromRGBAlphaHex(0x000000, 0.8);
    shadow.shadowOffset = CGSizeMake(0.5, 0.5);
    NSDictionary *attributesForChatContent = @{NSParagraphStyleAttributeName:paragraphStyle,NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:[UIColor YXColorWithHexCode:chatTextColorString],NSShadowAttributeName:shadow};
    [attributedString setAttributes:attributesForName range:nameRang];
    [attributedString setAttributes:attributesForChatContent range:chatTextRang];

    return attributedString;
}


//获取等级对应的图片
- (UIImageView *)showLevel:(NSString *)levelStr{
    UILabel * levelNum = [[UILabel alloc] init];
    levelNum.bounds = CGRectMake(1, 1, 10, 10);
    levelNum.textColor = [UIColor redColor];
    levelNum.text = levelStr;
    
    UIImageView *img = [[UIImageView alloc] init];
    [img addSubview:levelNum];
    
    if ([levelStr intValue] == 0){
        [img setImage:[UIImage imageNamed:@""]];

        return img;
        
    }else if ([levelStr intValue] >0 && [levelStr intValue] < 11) {
        [img setImage:[UIImage imageNamed:@"星星-实心"]];

        return img;
    }else if ([levelStr intValue] >=10 && [levelStr intValue] < 21){
        [img setImage:[UIImage imageNamed:@"星星-空心"]];

        return img;
    }else if ([levelStr intValue] >=21 && [levelStr intValue] < 31){
        
        [img setImage:[UIImage imageNamed:@"月亮-实心"]];

        return img;
    }else if ([levelStr intValue] >=31 && [levelStr intValue] < 41){
        
        [img setImage:[UIImage imageNamed:@"月亮-空心"]];

        return img;
    }else if ([levelStr intValue] >=41 && [levelStr intValue] < 51){
        
        [img setImage:[UIImage imageNamed:@"太阳-实心"]];

        return img;
    }else if ([levelStr intValue] >=51 && [levelStr intValue] < 61){
        
        [img setImage:[UIImage imageNamed:@"太阳-空心"]];

        return img;
    }else if ([levelStr intValue] >=61){
        
        [img setImage:[UIImage imageNamed:@"太阳-空心"]];

        return img;
    }
    return img;
}

- (void)changeGrabView{
    if (_delegate != nil&&[_delegate respondsToSelector:@selector (changeGrabView:)]) {
        [_delegate changeGrabView:self.entity];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    // Configure the view for the selected state
}

@end