This repository has been archived by the owner on Feb 20, 2021. It is now read-only.
/
LCUserFeedbackReplyCell.m
260 lines (219 loc) · 8.77 KB
/
LCUserFeedbackReplyCell.m
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//
// LCUserFeedbackCell.m
//
// Created by yang chaozhong on 4/24/14.
// Copyright (c) 2014 LeanCloud. All rights reserved.
//
#import "LCUserFeedbackReplyCell.h"
#import "LCUserFeedbackReply.h"
static CGFloat const kLabelVerticalPadding = 10;
static CGFloat const kTimestampLabelHeight = 18;
static CGFloat const kMaxiumCellWidth = 226;
static CGFloat const kBubbleHorizontalPadding = 10;
static CGFloat const kContentHorizontalPadding = 10;
static CGFloat const kContentVerticalPadding = 10;
static CGFloat const kBubbleArrowHeight = 14; /**< 气泡尖嘴的高度 */
@interface LCUserFeedbackReplyCell()<UIGestureRecognizerDelegate>
@property (nonatomic, strong) UILabel *timestampLabel;
@property (nonatomic, strong) UIImageView *bubbleImageView;
@property (nonatomic, strong) LCUserFeedbackReply *feedbackReply;
@end
@implementation LCUserFeedbackReplyCell
#pragma mark - life cycle
- (id)initWithFeedbackReply:(LCUserFeedbackReply *)reply reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
if (self) {
[self setupWithFeedbackReply:reply];
}
return self;
}
- (void)setupWithFeedbackReply:(LCUserFeedbackReply *)reply {
self.backgroundColor = [UIColor clearColor];
[self setupTextLabel];
[self setupImageView];
[self.contentView addSubview:self.timestampLabel];
switch (reply.contentType) {
case LCContentTypeText:
[self.contentView insertSubview:self.bubbleImageView belowSubview:self.textLabel];
break;
case LCContentTypeImage:
[self.contentView insertSubview:self.bubbleImageView belowSubview:self.imageView];
break;
}
self.selectionStyle = UITableViewCellSelectionStyleNone;
}
#pragma mark - property
- (void)setupTextLabel {
if ([[UIDevice currentDevice].systemVersion floatValue] < 7.0f) {
self.textLabel.backgroundColor = [UIColor whiteColor];
}
self.textLabel.font = [[LCUserFeedbackReplyCell appearance] cellFont];
self.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.textLabel.numberOfLines = 0;
self.textLabel.textAlignment = NSTextAlignmentLeft;
self.textLabel.textColor = [UIColor blackColor];
}
- (void)setupImageView {
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
self.imageView.clipsToBounds = YES;
self.imageView.userInteractionEnabled = YES;
}
- (UILabel *)timestampLabel {
if (_timestampLabel == nil) {
_timestampLabel = [[UILabel alloc] init];
_timestampLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_timestampLabel.textAlignment = NSTextAlignmentCenter;
_timestampLabel.backgroundColor = [UIColor clearColor];
_timestampLabel.font = [UIFont systemFontOfSize:9.0f];
_timestampLabel.textColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:1.0];
_timestampLabel.frame = CGRectMake(0.0f, kLabelVerticalPadding, self.bounds.size.width, kTimestampLabelHeight);
}
return _timestampLabel;
}
- (UIImageView *)bubbleImageView {
if (_bubbleImageView == nil) {
_bubbleImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
}
return _bubbleImageView;
}
#pragma mark - size or frame
+ (CGSize)getBubbleSizeWithReply:(LCUserFeedbackReply *)reply {
CGSize bubbleSize;
switch (reply.contentType) {
case LCContentTypeText: {
bubbleSize = [self caculateLabelSizeForText:reply.content];
break;
}
case LCContentTypeImage:
bubbleSize = CGSizeMake(120, 120);
break;
}
bubbleSize = CGSizeMake(bubbleSize.width + kContentHorizontalPadding * 2, bubbleSize.height + kContentVerticalPadding * 2);
bubbleSize.height += kBubbleArrowHeight;
return bubbleSize;
}
+ (CGFloat)heightForFeedbackReply:(LCUserFeedbackReply *)reply {
CGFloat height = kTimestampLabelHeight + 2 * kLabelVerticalPadding;
CGSize bubbleSize = [self getBubbleSizeWithReply:reply];
height += bubbleSize.height;
return height;
}
- (CGRect)bubbleFrame {
CGSize bubbleSize = [[self class] getBubbleSizeWithReply:self.feedbackReply];
CGFloat paddingX = kBubbleHorizontalPadding;
if (self.feedbackReply.type == LCReplyTypeUser) {
paddingX = CGRectGetWidth(self.bounds) - bubbleSize.width - kBubbleHorizontalPadding;
}
CGFloat originY = CGRectGetMaxY(self.timestampLabel.frame) + kLabelVerticalPadding;
return CGRectMake(paddingX, originY, bubbleSize.width, bubbleSize.height);
}
/**
* 气泡去除掉尖嘴的 frame
*/
- (CGRect)bubbleContentFrame {
CGRect contentFrame = [self bubbleFrame];
switch (self.feedbackReply.type) {
case LCReplyTypeDev: {
// 尖嘴在上方
contentFrame.origin.y += kBubbleArrowHeight;
contentFrame.size.height -= kBubbleArrowHeight;
}
break;
case LCReplyTypeUser:
// 尖嘴在下方
contentFrame.size.height -= kBubbleArrowHeight;
break;
}
return contentFrame;
}
#pragma mark - configuare
- (void)configuareCellWithFeedbackReply:(LCUserFeedbackReply *)reply {
for (UIGestureRecognizer *recognizer in self.contentView.gestureRecognizers) {
[self.imageView removeGestureRecognizer:recognizer];
}
self.feedbackReply = reply;
self.bubbleImageView.image = [self backgroundImageForFeedbackReply:reply];
self.timestampLabel.text = [self formatDateString:reply.createAt];
switch (reply.contentType) {
case LCContentTypeText:
self.textLabel.text = reply.content;
break;
case LCContentTypeImage:
self.imageView.image = reply.attachmentImage;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGestureRecognizer:)];
tap.delegate = self;
tap.cancelsTouchesInView = NO;
[self.imageView addGestureRecognizer:tap];
break;
}
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect bubbleFrame = [self bubbleFrame];
self.bubbleImageView.frame = bubbleFrame;
CGRect contentFrame = [self bubbleContentFrame];
CGRect innerContentFrame = CGRectMake(CGRectGetMinX(contentFrame) + kContentHorizontalPadding, CGRectGetMinY(contentFrame) + kContentVerticalPadding, CGRectGetWidth(contentFrame) - kContentHorizontalPadding * 2, CGRectGetHeight(contentFrame) - kContentVerticalPadding * 2);
switch (self.feedbackReply.contentType) {
case LCContentTypeText: {
self.textLabel.frame = innerContentFrame;
break;
}
case LCContentTypeImage: {
self.imageView.frame = innerContentFrame;
break;
}
}
}
- (void)prepareForReuse {
[super prepareForReuse];
self.timestampLabel.text = nil;
self.textLabel.text = nil;
self.imageView.image = nil;
self.bubbleImageView.image = nil;
}
#pragma mark - action
- (void)handleTapGestureRecognizer:(UITapGestureRecognizer *)tap {
if (tap.state == UIGestureRecognizerStateEnded) {
if ([self.delegate respondsToSelector:@selector(didSelectImageViewOnFeedbackReply:)]) {
[self.delegate didSelectImageViewOnFeedbackReply:self.feedbackReply];
}
}
}
#pragma mark - utils
+ (CGSize)caculateLabelSizeForText:(NSString *)text {
UILabel *gettingSizeLabel = [[UILabel alloc] init];
gettingSizeLabel.font = [[LCUserFeedbackReplyCell appearance] cellFont];
gettingSizeLabel.lineBreakMode = NSLineBreakByWordWrapping;
gettingSizeLabel.numberOfLines = 0;
gettingSizeLabel.text = text;
CGSize maxiumLabelSize = CGSizeMake(kMaxiumCellWidth, MAXFLOAT);
return [gettingSizeLabel sizeThatFits:maxiumLabelSize];
}
- (UIImage *)backgroundImageForFeedbackReply:(LCUserFeedbackReply *)reply {
if (reply.type == LCReplyTypeDev) {
return [[UIImage imageNamed:@"feedback_bg_1"] stretchableImageWithLeftCapWidth:20 topCapHeight:16];
} else {
return [[UIImage imageNamed:@"feedback_bg_2"] stretchableImageWithLeftCapWidth:1 topCapHeight:16];;
}
}
- (NSString *)formatDateString:(NSString *)dateString {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *date = [dateFormatter dateFromString:dateString];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
return [dateFormatter stringFromDate:date];
}
#pragma mark -
- (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
}
@end