×

iOS UITextField


UITextField is used to design textField. For Example , "Email" , "Password" or anything.

Important Properties :-

1) .text
2) .font
3) .numberOfLines
4) .lineBreakMode
5) .textColor
6) .backgroundColor
7) .textAlignment
8) .autoCorrectionType
9) .keyboardtype
10) .returnKeyType
11) .clearButtonMode
12) .userInteractionEnabled
13) .enabled

Example :-

    
    UITextField *emailTextField = [[UITextField alloc]initWithFrame:
   CGRectMake(12, 44, 220, 40)];
   
   [mainView addSubview:emailTextField];
    emailTextField.textColor = [UIColor blackColor];
    emailTextField.font = [UIFont fontWithName:@"Arial" size:16.0];
    emailTextField.placeholder = @"Email";
    emailTextField.textAlignment = NSTextAlignmentCenter;
    
 // This is the way to give border to any UI Element with the help of layer property.
    emailTextField.layer.borderWidth = 1.0;
    emailTextField.layer.borderColor = [UIColor grayColor].CGColor;
    emailTextField.layer.cornerRadius = 4.0;
    

Below Code itself is self explanatory. Firstly , we initialised a "emailTextField".Then added it to self.view and assigned it some placeholder and given textColor with "UIColor" class and font with "UIFont" class.

To give border Color and border width you can use layer Property as shown in example.

Output :-



UITextField with clear Button Mode



 emailTextField.clearButtonMode = UITextFieldViewModeWhileEditing;

.clearButtonMode property of UITextField is used to enable clear button while you start Editing. Remember While Editing :)

Delegates of Text Field :-

We will use it later when we perform search in later examples. These are the most used delegates of textField you must know.

 
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;

- (void)textFieldDidBeginEditing:(UnderlineTextField *)textField;

- (void)textFieldDidEndEditing:(UITextField *)textField;

-(BOOL)textFieldShouldReturn:(UnderlineTextField *)textField;