From 033f9053ada0a7f2a9ef7e6652746cd3cbb8f300 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 29 Sep 2012 16:15:01 -0400 Subject: Integrated multi-line SMS-style text box for chat pane. Added a tableView for chat messages. --- Classes/ChatViewController.m | 210 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 210 insertions(+) create mode 100644 Classes/ChatViewController.m (limited to 'Classes/ChatViewController.m') diff --git a/Classes/ChatViewController.m b/Classes/ChatViewController.m new file mode 100644 index 0000000..aee79c8 --- /dev/null +++ b/Classes/ChatViewController.m @@ -0,0 +1,210 @@ +// +// ChatViewController.m +// NearMe +// +// Created by TW on 9/29/12. +// Copyright 2012 __MyCompanyName__. All rights reserved. +// + +#import "ChatViewController.h" + + +@implementation ChatViewController + +@synthesize chatTableView = _chatTableView; + +-(id)init +{ + self = [super init]; + if(self){ + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(keyboardWillShow:) + name:UIKeyboardWillShowNotification + object:nil]; + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(keyboardWillHide:) + name:UIKeyboardWillHideNotification + object:nil]; + } + + return self; +} + + + +// Implement loadView to create a view hierarchy programmatically, without using a nib. +- (void)loadView { + self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]; + self.view.backgroundColor = [UIColor colorWithRed:219.0f/255.0f green:226.0f/255.0f blue:237.0f/255.0f alpha:1]; + + chatTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; + [self.view addSubview:chatTableView]; + + containerView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 40, 320, 40)]; + + textView = [[HPGrowingTextView alloc] initWithFrame:CGRectMake(6, 3, 240, 40)]; + textView.contentInset = UIEdgeInsetsMake(0, 5, 0, 5); + + textView.minNumberOfLines = 1; + textView.maxNumberOfLines = 6; + textView.returnKeyType = UIReturnKeyGo; //just as an example + textView.font = [UIFont systemFontOfSize:15.0f]; + textView.delegate = self; + textView.internalTextView.scrollIndicatorInsets = UIEdgeInsetsMake(5, 0, 5, 0); + textView.backgroundColor = [UIColor whiteColor]; + + // textView.text = @"test\n\ntest"; + // textView.animateHeightChange = NO; //turns off animation + + [self.view addSubview:containerView]; + + UIImage *rawEntryBackground = [UIImage imageNamed:@"MessageEntryInputField.png"]; + UIImage *entryBackground = [rawEntryBackground stretchableImageWithLeftCapWidth:13 topCapHeight:22]; + UIImageView *entryImageView = [[[UIImageView alloc] initWithImage:entryBackground] autorelease]; + entryImageView.frame = CGRectMake(5, 0, 248, 40); + entryImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; + + UIImage *rawBackground = [UIImage imageNamed:@"MessageEntryBackground.png"]; + UIImage *background = [rawBackground stretchableImageWithLeftCapWidth:13 topCapHeight:22]; + UIImageView *imageView = [[[UIImageView alloc] initWithImage:background] autorelease]; + imageView.frame = CGRectMake(0, 0, containerView.frame.size.width, containerView.frame.size.height); + imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; + + textView.autoresizingMask = UIViewAutoresizingFlexibleWidth; + + // view hierachy + [containerView addSubview:imageView]; + [containerView addSubview:textView]; + [containerView addSubview:entryImageView]; + + UIImage *sendBtnBackground = [[UIImage imageNamed:@"MessageEntrySendButton.png"] stretchableImageWithLeftCapWidth:13 topCapHeight:0]; + UIImage *selectedSendBtnBackground = [[UIImage imageNamed:@"MessageEntrySendButton.png"] stretchableImageWithLeftCapWidth:13 topCapHeight:0]; + + UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom]; + doneBtn.frame = CGRectMake(containerView.frame.size.width - 69, 8, 63, 27); + doneBtn.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin; + [doneBtn setTitle:@"Done" forState:UIControlStateNormal]; + + [doneBtn setTitleShadowColor:[UIColor colorWithWhite:0 alpha:0.4] forState:UIControlStateNormal]; + doneBtn.titleLabel.shadowOffset = CGSizeMake (0.0, -1.0); + doneBtn.titleLabel.font = [UIFont boldSystemFontOfSize:18.0f]; + + [doneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; + [doneBtn addTarget:self action:@selector(resignTextView) forControlEvents:UIControlEventTouchUpInside]; + [doneBtn setBackgroundImage:sendBtnBackground forState:UIControlStateNormal]; + [doneBtn setBackgroundImage:selectedSendBtnBackground forState:UIControlStateSelected]; + [containerView addSubview:doneBtn]; + containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin; +} + +-(void)resignTextView +{ + [textView resignFirstResponder]; +} + +//Code from Brett Schumann +-(void) keyboardWillShow:(NSNotification *)note{ + // get keyboard size and loctaion + CGRect keyboardBounds; + [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds]; + NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; + NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey]; + + // Need to translate the bounds to account for rotation. + keyboardBounds = [self.view convertRect:keyboardBounds toView:nil]; + + // get a rect for the textView frame + CGRect containerFrame = containerView.frame; + containerFrame.origin.y = self.view.bounds.size.height - (keyboardBounds.size.height + containerFrame.size.height); + // animations settings + [UIView beginAnimations:nil context:NULL]; + [UIView setAnimationBeginsFromCurrentState:YES]; + [UIView setAnimationDuration:[duration doubleValue]]; + [UIView setAnimationCurve:[curve intValue]]; + + // set views with new info + containerView.frame = containerFrame; + + // commit animations + [UIView commitAnimations]; +} + +-(void) keyboardWillHide:(NSNotification *)note{ + NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; + NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey]; + + // get a rect for the textView frame + CGRect containerFrame = containerView.frame; + containerFrame.origin.y = self.view.bounds.size.height - containerFrame.size.height; + + // animations settings + [UIView beginAnimations:nil context:NULL]; + [UIView setAnimationBeginsFromCurrentState:YES]; + [UIView setAnimationDuration:[duration doubleValue]]; + [UIView setAnimationCurve:[curve intValue]]; + + // set views with new info + containerView.frame = containerFrame; + + // commit animations + [UIView commitAnimations]; +} + +- (void)growingTextView:(HPGrowingTextView *)growingTextView willChangeHeight:(float)height +{ + float diff = (growingTextView.frame.size.height - height); + + CGRect r = containerView.frame; + r.size.height -= diff; + r.origin.y += diff; + containerView.frame = r; +} + +-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation +{ + return YES; +} + +- (void)didReceiveMemoryWarning { + // Releases the view if it doesn't have a superview. + [super didReceiveMemoryWarning]; + + // Release any cached data, images, etc that aren't in use. +} + +- (void)viewDidUnload { + // Release any retained subviews of the main view. + // e.g. self.myOutlet = nil; +} + + +- (void)dealloc { + [chatTableView release]; + [textView release]; + [containerView release]; + [super dealloc]; +} + +# pragma mark UITableViewDelegate methods + +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { + return 5; +} + +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { + static NSString *CellIdentifier = @"ChatCell"; + + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; + if (cell == nil) { + cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; + } + + // Set up the cell... + NSString *cellValue = @"test"; + cell.textLabel.text = cellValue; + + return cell; +} + +@end -- cgit v1.2.3