Home Tutorial Iphone Examples iphone TextView

 
 

Share on Google+Share on Google+
iphone TextView
Posted on: September 25, 2009 at 12:00 AM
Advertisement
In this iPhone tutorial learn how to use iPhone text view and also when to use text view, here will do every thing with coding i.e. not through the interface builder. Application will be View Based Application, will do our coding in view controller file i.e. .h and .m files.

iphone TextView 

In this tutorial will learn how to use iPhone text view and also when to use text view, here will do every thing with coding i.e. not through the interface builder. Application will be View Based Application, will do our coding in view controller file i.e. .h and .m files. Text field is used to show number of lines into it i.e. can type more than two lines and that will be visible but in case of text field we can type many lines but we can see only one line.

Final Image will look like this:

Here 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. Notice that it is marked with IBOutlet keyword, telling the interface Builder to make it available, so a connection can be created with the label on the view. Don't forget to synthesize and release the label's memory in the dealloc method. 

Add this to view controller.h file:

UITextView *textView;
@property (nonatomic, retain) UITextView *textView;
Synthesize is used to auto generate the get/set functions for a property based on its attributes declared (nonatomic and retain). It will generate the following functions to get and set the member and release the label's memory in the dealloc method. Now will setup the text view, in that will create frame, text Color, font and the background color of the text view. After that will write the text, which we want us to display on to the view or screen, will also set the keyboard type and the scroll bar so that we can see the text if it consist of many lines. In view did load method will set the title. After all this done will have to write the code for the keyboard to show when we click on to the view and also the method to hide it, will provide the save button so that by clicking on the button the keyboard will hide and the button is set on the right side of the navigation bar.

Add this to view controller.m file:

@synthesize textView;

-(void)dealloc

{

    [textView release];

    [super dealloc];

}

-(void)setupTextView

{

    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 = @"Hello this is about the text view, the difference in text view and the text field is that you can display large data or paragraph in text view but in text field you cant.";

    self.textView.returnKeyType = UIReturnKeyDefault;

    self.textView.keyboardType = UIKeyboardTypeDefault; 

    self.textView.scrollEnabled = YES;

    self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;

    [self.view addSubview: self.textView];  // adding the text view to the view.

}

-(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];

}

- (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;  // this will remove the "save" button

}

Finally press Build And Go Button

Download Here

Advertisement

Related Tags for iphone TextView:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 25, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com