This is the simple TextView example. We will learn how to use Text View, here will do every thing with coding and we can easily edit , delete , add content in the text view .
Step 1: Create a View-base application using the template. Give the name of the project TextView Application.
Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.
Step 3: Expand classes and notice Interface Builder created the TextView_ApplicationViewController class for you. Expand Resources and notice the template generated a separate nib, SimpleExampleViewController.xib for the TextView_ApplicationViewController.
Step 4: Make the following changes in the AppDelegate.m file.
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
[window addSubview:navController.view];
[window makeKeyAndVisible];
}
Step 5: We will create variable for the text view and after creating or declaring the variable for the text view will write the property for that,We have also declared an object of type UITextView called textview which will be connected to the label on the view. So make the following changes in the TextView_ApplicationViewController.h file .
@interface TextView_ApplicationViewController : UIViewController <UITextViewDelegate>
{
UITextView *textView;
}
@property (nonatomic, retain) UITextView *textView;
Step 6: Now do the following changes in the TextView_ApplicationViewController.h file .
{
self.textView = [[[UITextView alloc] initWithFrame:self.view.frame] autorelease];
self.textView.textColor = [UIColor blackColor];
self.textView.font = [UIFont fontWithName:@"Arial" size:18];
self.textView.delegate = self;
self.textView.backgroundColor = [UIColor whiteColor];
self.textView.text = @"This is the text view example,we can edit , delete, add content in the text view.";
self.textView.returnKeyType = UIReturnKeyDefault;
self.textView.keyboardType = UIKeyboardTypeDefault;
self.textView.scrollEnabled = YES;
self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self.view addSubview: self.textView];
}
-(void)viewDidLoad
{
[super viewDidLoad];
self.title = NSLocalizedString(@"TextViewTitle", @"");
[self setuptextView];
}
-(void)viewDidUnload
{
[super viewDidUnload];
self.textView = nil;
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
-(void)keyboardWillShow:(NSNotification *)aNotification
{
CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = self.view.frame;
frame.size.height -= keyboardRect.size.height;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
}
-(void)keyboardWillHide:(NSNotification *)aNotification
{
CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = self.view.frame;
frame.size.height += keyboardRect.size.height;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
}
#pragma mark -
#pragma mark UItextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView
{
UIBarButtonItem* saveItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:@selector(saveAction:)];
self.navigationItem.rightBarButtonItem = saveItem;
[saveItem release];
}
- (void)saveAction:(id)sender
{
[self.textView resignFirstResponder];
self.navigationItem.rightBarButtonItem = nil;
}
Step 6: Now compile and run the application in the simulator.
You can downloaded SourceCode from here TextView Application 2











