8

how to set uitextfield and UITextView border color programmatically , when we enter or edit in textfield and textview.

I used this code, but doesn't change the border color for the UITextView.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    textField.layer.borderColor=[[UIColor cyanColor] CGColor];
}
Ash Furrow
  • 12,391
  • 3
  • 57
  • 92
raman
  • 175
  • 1
  • 2
  • 14
  • Have you included QuartzCore framework into your build phases? – Ste Prescott Mar 22 '13 at 14:14
  • @ste prescott yes i included quartzCore framework into my build phases. but when i edit the text field it should not change the border color. – raman Mar 22 '13 at 14:18

4 Answers4

25

Don't Forget : #Import <QuartzCore/QuartzCore.h>

Working Code :

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    textField.layer.cornerRadius=8.0f;
    textField.layer.masksToBounds=YES;
    textField.layer.borderColor=[[UIColor redColor]CGColor];
    textField.layer.borderWidth= 1.0f;
    return YES;
}
Bhavin
  • 27,155
  • 11
  • 55
  • 94
1
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

declares if the user is allowed to edit a text field. Change the method to:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
  textField.layer.borderColor=[[UIColor cyanColor] CGColor];
}
Sam Baumgarten
  • 2,231
  • 3
  • 21
  • 46
mr.VVoo
  • 2,265
  • 20
  • 30
  • i use the uiborderstyleline for uitextfield. so, how to set border color to uiborderstyleline of uitextfield. – raman Mar 22 '13 at 14:37
1

For give Border and Color Of UITextField.

Add #import "QuartzCore/QuartzCore.h" fram work.

textField.layer.borderColor = [UIColor lightGrayColor].CGColor; // set color as you want.
textField.layer.borderWidth = 1.0; // set borderWidth as you want.
iPatel
  • 46,010
  • 16
  • 115
  • 137
  • i use the uiborderstyleline for uitextfield. so, how to set border color to uiborderstyleline of uitextfield. – raman Mar 22 '13 at 14:42
0

When you edit a text field, there are a lot of methods that seem really similar. You are trying to use-textFieldShouldBeginEditing:. According to the documentation, textFieldShouldBeginEditing, "Asks the delegate if editing should begin in the specified text field." The usage is, "When the user performs an action that would normally initiate an editing session, the text field calls this method first to see if editing should actually proceed. In most circumstances, you would simply return YES from this method to allow editing to proceed." This is not what you wan't to do.

Instead you should use -textFieldDidBeginEditing:. This method, "Tells the delegate that editing began for the specified text field." It, "notifies the delegate that the specified text field just became the first responder. You can use this method to update your delegate’s state information. For example, you might use this method to show overlay views that should be visible while editing."

This means your code should change from:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    textField.layer.borderColor=[[UIColor cyanColor] CGColor];
}

to

-(BOOL)textFieldDidBeginEditing:(UITextField *)textField {
    textField.layer.borderColor=[[UIColor cyanColor] CGColor];
}

You can read more about the UITextFieldDelegate methods in the docs at http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html

Sam Baumgarten
  • 2,231
  • 3
  • 21
  • 46
  • i use the uiborderstyleline for uitextfield. so, how to set border color to uiborderstyleline of uitextfield. – raman Mar 22 '13 at 14:40