IVTChatRoomCell.m
9.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//
// 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