Att Ios Ui Text Field
## Using Text Fields
A text field is a user interface element used to obtain user input through an application.
A UITextField looks like this:

Important properties of a text field:
* Displays placeholder text when there is no user input
* Normal text
* Autocorrection type
* Keyboard type
* Return key type
* Clear button mode
* Alignment
* Delegate
* * *
## Updating Attributes in XIB
You can change the text field attributes in the Attribute Inspector within the Utility area (the right side of the window) of the XIB file.

* * *
## Text Field Delegate
We can set the delegate by right-clicking the UIElement in Interface Builder and connecting it to the File's Owner, as shown below:

Steps to use the delegate:
* 1. Set the delegate as shown in the image above.
* 2. Add the delegate to your response class.
* 3. Implement the text field delegate methods. Important text field delegate methods are:
- (void)textFieldDidBeginEditing:(UITextField *)textField
- (void)textFieldDidEndEditing:(UITextField *)textField
* 4. As their names imply, the two delegate methods above are called when editing begins and ends in the text field, respectively.
* 5. For other delegate methods, please refer to the UITextDelegate Protocol reference manual.
* * *
## Example
Below, we use a simple example to create UI elements.
The ViewController class will adopt the UITextFieldDelegate protocol. Modify the ViewController.h file as follows:
Add the method `addTextField` to our ViewController.m file.
Then call this method in the `viewDidLoad` method.
Update `viewDidLoad` in ViewController.m as follows:
```objective-c
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
;
//The custom method to create our textfield is called
;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
;
// Dispose of any resources that can be recreated.
}
-(void)addTextField{
// This allocates a label
UILabel *prefixLabel = [initWithFrame:CGRectZero];
//This sets the label text
prefixLabel.text =@"## ";
// This sets the font for the label
[prefixLabel setFont:[UIFont boldSystemFontOfSize:14]];
// This fits the frame to size of the text
;
// This allocates the textfield and sets its frame
UITextField *textField = [ initWithFrame: CGRectMake(20, 50, 280, 30)];
// This sets the border style of the text field
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[textField setFont:[UIFont boldSystemFontOfSize:12]];
//Placeholder text is displayed when no text is typed
textField.placeholder = @"Simple Text field";
//Prefix label is set as left view and the text starts after that
textField.leftView = prefixLabel;
//It set when the left prefixLabel to be displayed
textField.leftViewMode = UITextFieldViewModeAlways;
// Adds the textField to the view.
[self.view addSubview:textField];
// sets the delegate to the current class
textField.delegate = self;
}
// pragma mark is used for easy access of code in Xcode
#pragma mark - TextField Delegates
// This method is called once we click inside the textField
-(void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"Text field did begin editing");
}
// This method is called once we complete editing
-(void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"Text field ended editing");
}
// This method enables or disables the processing of return key
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
;
return YES;
}
- (void)viewDidUnload {
label = nil;
;
}
@end
Running the application will produce the following output:

The delegate methods are called based on user actions. To know when the delegate is called, please refer to the console output.
YouTip