-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatTableViewCell.m
executable file
·198 lines (152 loc) · 5.76 KB
/
ChatTableViewCell.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
//
// ChatTableViewCell.m
// ChatDemo
//
// Created by Charlene Jiang on 10/11/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "ChatTableViewCell.h"
@interface UITextView (Chat)
- (BOOL)canBecomeFirstResponder;
@end
@implementation UITextView (Chat)
- (BOOL)canBecomeFirstResponder
{
return NO;
}
@end
@implementation ChatTableViewCell
#define DATE_LABEL_TAG 300
#define IMAGE_BUTTON_TAG 301
#define BUBBLE_VIEW_TAG 302
#define BUBBLE_IMAGE_VIEW_TAG 303
#define BUBBLE_LABEL_TAG 304
#define LEFT_CAP_HEIGHT 14
#define TOP_CAP_HEIGHT 25
@synthesize profile, msg;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
/*
UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backView.backgroundColor = [UIColor clearColor];
self.backgroundView = backView;
*/
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)drawCell
{
self.userInteractionEnabled=NO;
UILabel *dateLabel = [self viewWithTag:DATE_LABEL_TAG];
if (dateLabel == nil) {
dateLabel = [[[UILabel alloc] initWithFrame:CGRectMake(8, 2, 70, 10)] autorelease];
dateLabel.textColor = [UIColor darkGrayColor];
dateLabel.backgroundColor = [UIColor clearColor];
dateLabel.font = [UIFont systemFontOfSize:10];
dateLabel.tag = DATE_LABEL_TAG;
[self addSubview:dateLabel];
if (msg.isMe) {
CGRect rect = dateLabel.frame;
rect.origin.x = 320 - dateLabel.frame.size.width - 8;
dateLabel.frame = rect;
}
}
//
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM-dd hh:mm:ss"];
dateLabel.text = [formatter stringFromDate:msg.messageDate];
[formatter release];
//
UIButton *imgButton = [self viewWithTag:IMAGE_BUTTON_TAG];
if (imgButton == nil) {
imgButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
imgButton.frame = CGRectMake(8, 14, 50, 50);
[imgButton setBackgroundImage:profile.userPhoto forState:UIControlStateNormal];
[imgButton addTarget:self action:@selector(imgButtonAction:) forControlEvents:UIControlEventTouchUpInside];
imgButton.tag = IMAGE_BUTTON_TAG;
[self addSubview:imgButton];
if (msg.isMe) {
CGRect rect = imgButton.frame;
rect.origin.x = 320 - imgButton.frame.size.width - 8;
imgButton.frame = rect;
}
}
UIView *bubbleView = [self viewWithTag:BUBBLE_VIEW_TAG];
if (bubbleView == nil) {
bubbleView = [[UIView alloc] initWithFrame:CGRectZero];
bubbleView.backgroundColor = [UIColor clearColor];
//
UIImage *bubble = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:msg.isMe?@"popo_02":@"popo_01" ofType:@"gif"]];
UIImageView *bubbleImageView = [[UIImageView alloc] initWithImage:[bubble stretchableImageWithLeftCapWidth:LEFT_CAP_HEIGHT topCapHeight:TOP_CAP_HEIGHT]];
bubbleImageView.tag = BUBBLE_IMAGE_VIEW_TAG;
bubbleImageView.backgroundColor = [UIColor clearColor];
[bubbleView addSubview:bubbleImageView];
[bubbleImageView release];
//
UITextView *bubbleText = [[UITextView alloc] initWithFrame:CGRectMake(12, 4, 150, 50)];
bubbleText.backgroundColor = [UIColor clearColor];
bubbleText.font = [UIFont systemFontOfSize:14];
bubbleText.textColor = [UIColor blackColor];
bubbleText.scrollEnabled = NO;
bubbleText.editable = NO;
bubbleText.delegate = self;
bubbleText.tag = BUBBLE_LABEL_TAG;
[bubbleView addSubview:bubbleText];
[bubbleText release];
//
bubbleView.tag = BUBBLE_VIEW_TAG;
[self addSubview:bubbleView];
}
//
UIFont *font = [UIFont systemFontOfSize:14];
CGSize size = [msg.message sizeWithFont:font constrainedToSize:CGSizeMake(150.0f, 1000.0f) lineBreakMode:UILineBreakModeCharacterWrap];
//
UITextView *bubbleText = (UITextView *)[bubbleView viewWithTag:BUBBLE_LABEL_TAG];
if (msg.isMe) {
bubbleText.frame = CGRectMake(2, 1, size.width + 20, size.height + 20);
} else {
bubbleText.frame = CGRectMake(10, 1, size.width + 20, size.height + 20);
}
bubbleText.text = msg.message;
//
CGFloat imageWidth = bubbleText.frame.size.width + 12;
CGFloat imageHeight = bubbleText.frame.size.height + 2;
UIImageView *bubbleImageView = (UIImageView *)[bubbleView viewWithTag:BUBBLE_IMAGE_VIEW_TAG];
bubbleImageView.frame = CGRectMake(0.0f, 0.0f, imageWidth, imageHeight);
//
if(msg.isMe) {
bubbleView.frame = CGRectMake(320.0f - imageWidth - 60.0f, 14.0f, imageWidth, imageHeight);
} else {
bubbleView.frame = CGRectMake(60.0f, 14.0f, imageWidth, imageHeight);
}
}
- (void)imgButtonAction:(id)sender
{
NSLog(@"imgButtonAction");
}
+ (float)heightOfCellWithMessage:(NSString *) message
{
UIFont *font = [UIFont systemFontOfSize:14];
CGSize size = [message sizeWithFont:font constrainedToSize:CGSizeMake(150.0f, 1000.0f) lineBreakMode:UILineBreakModeCharacterWrap];
return size.height + 20 + 2 + 16;
}
//
//- (void)textViewDidChangeSelection:(UITextView *)textView
//{
// NSLog(@"textViewDidChangeSelection");
//}
//
//- (void)selectionWillChange:(id <UITextInput>)textInput
//{
// NSLog(@"selectionWillChange");
//}
@end