Skip to content

Instantly share code, notes, and snippets.

@romyilano
Created January 3, 2013 16:41
Show Gist options
  • Select an option

  • Save romyilano/4444792 to your computer and use it in GitHub Desktop.

Select an option

Save romyilano/4444792 to your computer and use it in GitHub Desktop.
This makes editing the textview fill up the entire screen
//
// ViewController.m
// erica-Sadun-textViewExample
//
// Created by Romy Ilano on 1/3/13.
// Copyright (c) 2013 Romy Ilano. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// add the notification
// I'm going to listen for when the keyboard "did show" and "will hide"
// offers opportune times to react to the keyboard arriving onscreen or preparing to leave
// each notification provides a userInfo dictionary that supplies the bounds for the keyboard, using the UIKeyboardBoundsUserInfoKey
// no objects are passed around with the notification. you are not granted direct access to the keyboard itself
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(adjustForKeyboard:)
name:UIKeyboardDidShowNotification
object:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark-TextViewMethods
CGRect CGRectShrinkHeight(CGRect rect, CGFloat amount)
{
return CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height-amount);
}
-(void)keyboardWillHide:(NSNotification *)notification
{
// return to the previous text view size
self.tv.frame=self.view.bounds;
}
// see that notification? it comes baked with a userInfo dictionary that i retrieve [notification userInfo]
-(void)adjustForKeyboard:(NSNotification *)notification
{
// retrive the keyboard bounds via the notification userInfo dictionary
// this lets me resize the views to adapt them to the keyboard's presence
CGRect kbounds;
NSDictionary *userInfo = [notification userInfo];
// standard keyboards always appear on the bottom of the screen... generally when a text-capable view becomes the first responder, custom keyboards are not limited to that style of presentation
[(NSValue *)[userInfo objectForKey:@"UIKeyboardBoundsUserInfoKey"] getValue:&kbounds];
// shrink the textview frame
CGRect destRect = CGRectShrinkHeight(self.view.bounds, kbounds.size.height);
self.tv.frame=destRect;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment