iphone TextView


 

iphone TextView

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.

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

0

    self.title = NSLocalizedString(@"TextViewTitle", @"");    

    [self setupTextView];

}

1

-(void)viewDidUnload

{

    [super viewDidUnload];

2

    self.textView = nil;

}

-(void)viewWillAppear:(BOOL)animated

3

{

    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

4

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

}

-(void)viewDidDisappear:(BOOL)animated

5

{

    [super viewDidDisappear:animated];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];

6

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

}

-(void)keyboardWillShow:(NSNotification *)aNotification

7

{

    CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];

    NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

8

    CGRect frame = self.view.frame;

    frame.size.height -= keyboardRect.size.height;

    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];

9

    [UIView setAnimationDuration:animationDuration];

    self.view.frame = frame;

    [UIView commitAnimations];

0

}

-(void)keyboardWillHide:(NSNotification *)aNotification

{

1

    CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];

    NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    CGRect frame = self.view.frame;

2

    frame.size.height += keyboardRect.size.height;

    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];

    [UIView setAnimationDuration:animationDuration];

3

    self.view.frame = frame;

    [UIView commitAnimations];

}

4

- (void)textViewDidBeginEditing:(UITextView *)textView

{

    UIBarButtonItem* saveItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone

5

                                      target:self action:@selector(saveAction:)];

    self.navigationItem.rightBarButtonItem = saveItem;

    [saveItem release];

6

}

- (void)saveAction:(id)sender

{

7

    [self.textView resignFirstResponder];

    self.navigationItem.rightBarButtonItem = nil;  // this will remove the "save" button

}

8

Finally press Build And Go Button

Download Here

9

Ads