YH_BarrageMsgCell.m
7.28 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
//
// 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 6
#define kMarginBottom 6
#define kPandingV 2
#define kPandingH 10
#define kUserAvatarWidth 35
#define kUserNameLableMaxWidth (245 * kScreenPointScale - 85)
#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];
CGSize msgSize = [msg.message boundingRectWithSize:CGSizeMake(kUserNameLableMaxWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : kFontUserMsg} context:nil].size;
NSString *nameLabelText = [NSString stringWithFormat:@"@%@", msg.userName];
CGSize nameSize = [nameLabelText boundingRectWithSize:CGSizeMake(kUserNameLableMaxWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : kFontUserName} context:nil].size;
[self.avatarImageView setFrame:CGRectMake(-1, 0, kUserAvatarWidth, cellHeight - kContentMarginTop)];
[self.nameLabel setFrame:CGRectMake(CGRectGetWidth(self.avatarImageView.frame)+kPandingH, kMarginTop, nameSize.width, nameSize.height)];
[self.msgLabel setFrame:CGRectMake(CGRectGetWidth(self.avatarImageView.frame)+kPandingH, CGRectGetMaxY(self.nameLabel.frame)+kPandingV, msgSize.width, msgSize.height)];
CGFloat bgwidth = CGRectGetMaxX(self.msgLabel.frame) + 10;
if (nameSize.width > msgSize.width) {
bgwidth = CGRectGetMaxX(self.nameLabel.frame) + 10;
}
[self.bgview setFrame:CGRectMake(kMarginLeft, kContentMarginTop, bgwidth, cellHeight - kContentMarginTop)];
}
- (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;
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