Friday, October 8, 2010

UITextField Overview

In my last blog i have explained how to work with Xcode by showing a small demonstration on UITextField, in this post i will cover some of the methods and properties of Textfield, so lets begin

Open Xcode and display a textfield in the window of iPhone simulator in case you forgot how to do that hers the link to my blog which will help you (TextField display).

The UITextField is just like a textbox where the user enters data via keyboard and is the most commonly used UIControl (UI stands for User Interface) UITextField is a class in the UIKit framework which contains lots of controls with the help of which you can design your iPhone app. Alright now we have some basic understanding about UITextfield so lets begin

Setting the text
There are two ways by which you can set the text of TextField the first method is by property called text and second method is by using an object function called setText.

To use them you have to make the object of UITextField and then you can use any one of the methods

[objtxtfield setText:@"Radix"];
objtxtfield.text = @"Radix";

both will give you the same output which will look like this

Place Holder
setPlaceholder is a method which will display a text by default prior to the user entering anything and you can use this method like this

[objtxtfield setPlaceholder:@"iphone by radix"];

which will give you the following output

and when the user will start editing the textfield then the default text will disappear and if the textfield contains no data then the user can see the default text in this case it will be iphone by radix.

you might want the data to appear in your text field with a specific font and size so in this case you can use the setFont method.

[objtxtfield setFont:[UIFont fontWithName:@"Georgia" size:25]];

which will make the text look something like this


you can set where the text must appear like to left, right, or center with the help of the property textAlignment

objtxtfield.textAlignment = UITextAlignmentCenter;

and this will make the text look like this


now there might be a time when you want the textfield to be read only so in this case you have the setUserInteractionEnabled boolean method and you can use this object method like this

[objtxtfield setUserInteractionEnabled:NO];

also there might be a scenario where you might want the text to be cleared as soon as the user begins editing the textfield so in that case you can use the clearsOnBeginEditing property and set it to YES

Display image in textfield

Now let's say you want to display an image in a textfield

UIImageView *objimage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"apple.png"]];
[objtxtfield setLeftView:objimage];
[objtxtfield setLeftViewMode:UITextFieldViewModeAlways];

When the above code lines executes you will get the following result in your simulator

Adjusting the text size in UITextField

Text displayed in the textfield can be dynamically sized based upon the width of the textfield. The text being typed will be available on the screen and will keep on shrinking down upto a default limit of font size 17. So here i make my font size huge like some what of 30 in size and then i start typing in my text field.

objtxtfield.text = @"Large text";
[objtxtfield setFont:[UIFont fontWithName:@"Georgia" size:30]];
[objtxtfield setAdjustsFontSizeToFitWidth:YES];

now if you do this you application at start will look like this

and once you start typing on the textfield it will automatically shrink its size and then you will have this view given below

Hiding and showing the keyboard

For this you have a protocol method named textFieldShouldReturn

- (BOOL)textFieldShouldReturn:(UITextField *)textField
all you have to do is first set the delegate property of the object of textfield to self

objtxtfield.delegate = self;

and then in the textFieldShouldReturn method you have to write the following code

- (BOOL)textFieldShouldReturn:(UITextField *)textField
[objtxtfield resignFirstResponder];
return YES;

and in order to show the keyboard you have to write the following code

[objtxtfield becomeFirstResponder];

Well i hope you enjoyed reading this post on UITextField, thanks for reading this blog and Happy iCoding.

1 comment: