forked from soffes/ssmessagesviewcontroller
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSSMessagesViewController.m
More file actions
171 lines (130 loc) · 6.83 KB
/
Copy pathSSMessagesViewController.m
File metadata and controls
171 lines (130 loc) · 6.83 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
//
// SSMessagesViewController.m
// Messages
//
// Created by Sam Soffes on 3/10/10.
// Copyright 2010-2011 Sam Soffes. All rights reserved.
//
#import "SSMessagesViewController.h"
#import "SSMessageTableViewCell.h"
#import "SSMessageTableViewCellBubbleView.h"
#import <SSToolkit/SSTextField.h>
CGFloat kInputHeight = 40.0f;
@implementation SSMessagesViewController
@synthesize tableView = _tableView;
@synthesize inputBackgroundView = _inputBackgroundView;
@synthesize textField = _textField;
@synthesize sendButton = _sendButton;
@synthesize leftBackgroundImage = _leftBackgroundImage;
@synthesize rightBackgroundImage = _rightBackgroundImage;
#pragma mark NSObject
- (void)dealloc {
self.leftBackgroundImage = nil;
self.rightBackgroundImage = nil;
[_tableView release];
[_inputBackgroundView release];
[_textField release];
[_sendButton release];
[super dealloc];
}
#pragma mark UIViewController
- (void)viewDidLoad {
self.view.backgroundColor = [UIColor colorWithRed:0.859f green:0.886f blue:0.929f alpha:1.0f];
CGSize size = self.view.frame.size;
// Table view
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, size.width, size.height - kInputHeight) style:UITableViewStylePlain];
_tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_tableView.backgroundColor = self.view.backgroundColor;
_tableView.dataSource = self;
_tableView.delegate = self;
_tableView.separatorColor = self.view.backgroundColor;
[self.view addSubview:_tableView];
// Input
_inputBackgroundView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, size.height - kInputHeight, size.width, kInputHeight)];
_inputBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
_inputBackgroundView.image = [UIImage imageNamed:@"SSMessagesViewControllerInputBackground.png"];
_inputBackgroundView.userInteractionEnabled = YES;
[self.view addSubview:_inputBackgroundView];
// Text field
_textField = [[SSTextField alloc] initWithFrame:CGRectMake(6.0f, 0.0f, size.width - 75.0f, kInputHeight)];
_textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_textField.backgroundColor = [UIColor whiteColor];
_textField.background = [[UIImage imageNamed:@"SSMessagesViewControllerTextFieldBackground.png"] stretchableImageWithLeftCapWidth:12 topCapHeight:0];
_textField.delegate = self;
_textField.font = [UIFont systemFontOfSize:15.0f];
_textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
_textField.textEdgeInsets = UIEdgeInsetsMake(4.0f, 12.0f, 0.0f, 12.0f);
[_inputBackgroundView addSubview:_textField];
// Send button
_sendButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
_sendButton.frame = CGRectMake(size.width - 65.0f, 8.0f, 59.0f, 27.0f);
_sendButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
_sendButton.titleLabel.font = [UIFont boldSystemFontOfSize:16.0f];
_sendButton.titleLabel.shadowOffset = CGSizeMake(0.0f, -1.0f);
[_sendButton setBackgroundImage:[[UIImage imageNamed:@"SSMessagesViewControllerSendButtonBackground.png"] stretchableImageWithLeftCapWidth:12 topCapHeight:0] forState:UIControlStateNormal];
[_sendButton setTitle:@"Send" forState:UIControlStateNormal];
[_sendButton setTitleColor:[UIColor colorWithWhite:1.0f alpha:0.4f] forState:UIControlStateNormal];
[_sendButton setTitleShadowColor:[UIColor colorWithRed:0.325f green:0.463f blue:0.675f alpha:1.0f] forState:UIControlStateNormal];
[_inputBackgroundView addSubview:_sendButton];
self.leftBackgroundImage = [[UIImage imageNamed:@"SSMessageTableViewCellBackgroundClear.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:14];
self.rightBackgroundImage = [[UIImage imageNamed:@"SSMessageTableViewCellBackgroundGreen.png"] stretchableImageWithLeftCapWidth:17 topCapHeight:14];
}
#pragma mark SSMessagesViewController
// This method is intended to be overridden by subclasses
- (SSMessageStyle)messageStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return SSMessageStyleLeft;
}
// This method is intended to be overridden by subclasses
- (NSString *)textForRowAtIndexPath:(NSIndexPath *)indexPath {
return nil;
}
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"cellIdentifier";
SSMessageTableViewCell *cell = (SSMessageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[SSMessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
[cell setBackgroundImage:self.leftBackgroundImage forMessageStyle:SSMessageStyleLeft];
[cell setBackgroundImage:self.rightBackgroundImage forMessageStyle:SSMessageStyleRight];
}
cell.messageStyle = [self messageStyleForRowAtIndexPath:indexPath];
cell.messageText = [self textForRowAtIndexPath:indexPath];
return cell;
}
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [SSMessageTableViewCellBubbleView cellHeightForText:[self textForRowAtIndexPath:indexPath]];
}
#pragma mark UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[UIView beginAnimations:@"beginEditing" context:_inputBackgroundView];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.3f];
_tableView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, 216.0f, 0.0f);
_tableView.scrollIndicatorInsets = _tableView.contentInset;
_inputBackgroundView.frame = CGRectMake(0.0f, 160.0f, self.view.frame.size.width, kInputHeight);
[_sendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[UIView commitAnimations];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[_tableView numberOfRowsInSection:0]-1
inSection:0];
[_tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionBottom
animated:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[UIView beginAnimations:@"endEditing" context:_inputBackgroundView];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.3f];
_tableView.contentInset = UIEdgeInsetsZero;
_tableView.scrollIndicatorInsets = UIEdgeInsetsZero;
_inputBackgroundView.frame = CGRectMake(0.0f, _tableView.frame.size.height, self.view.frame.size.width, kInputHeight);
[_sendButton setTitleColor:[UIColor colorWithWhite:1.0f alpha:0.4f] forState:UIControlStateNormal];
[UIView commitAnimations];
}
@end