// // GrowingTextViewExampleViewController.m // // Created by Hans Pinckaers on 29-06-10. // // MIT License // // Copyright (c) 2011 Hans Pinckaers // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. #import "GrowingTextViewExampleViewController.h" @implementation GrowingTextViewExampleViewController -(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]]; self.view.backgroundColor = [UIColor colorWithRed:219.0f/255.0f green:226.0f/255.0f blue:237.0f/255.0f alpha:1]; containerView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 40, 320, 40)]; textView = [[HPGrowingTextView alloc] initWithFrame:CGRectMake(6, 3, 240, 40)]; textView.isScrollable = NO; textView.contentInset = UIEdgeInsetsMake(0, 5, 0, 5); textView.minNumberOfLines = 1; textView.maxNumberOfLines = 6; // you can also set the maximum height in points with maxHeight // textView.maxHeight = 200.0f; 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.placeholder = @"Type to see the textView grow!"; // 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]; 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]; 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; } @end