24

I want to change UITextField border color. Is it possible to customize the color of border? I searched all options in xib but I did not found any option to change border color.

Mudriy Ilya
  • 122
  • 2
  • 11
user2017285
  • 283
  • 1
  • 2
  • 10
  • 3
    sorry when i searched for textfield border color change i dint found that post so i posted otherwise i would have not. – user2017285 Mar 01 '13 at 09:15

3 Answers3

49

you can use this:

yourTextField.layer.borderColor=[[UIColor blackColor]CGColor];

yourTextField.layer.borderWidth=1.0;

and remember to import #import <QuartzCore/QuartzCore.h> in your .h file

You can also specify your RGB value.

yourTextField.layer.borderColor=[[UIColor colorWithRed:178.0f/255.0f green:178.0f/255.0f blue:178.0f/255.0f alpha:1.0] CGColor];

Note: you need to set both values starting with iOS 7

Update for swift 2.3

yourTextField.layer.borderColor = UIColor.blackColor().CGColor
yourTextField.layer.borderWidth = 1.0

OR

yourTextField.layer.borderColor = UIColor(red: 178.0 / 255.0, green: 178.0 / 255.0, blue: 178.0 / 255.0, alpha: 1.0).CGColor

Update for swift 3.1.1

yourTextField.layer.borderColor = UIColor.black.cgColor
yourTextField.layer.borderWidth = 1.0

OR

yourTextField.layer.borderColor = UIColor(red: 178.0 / 255.0, green: 178.0 / 255.0, blue: 178.0 / 255.0, alpha: 1.0).cgColor
KDeogharkar
  • 10,939
  • 7
  • 51
  • 95
12
#import <QuartzCore/QuartzCore.h>

Use below code to change Textfield's border color

textField.layer.borderWidth = 2.0f;
textField.layer.borderColor = [[UIColor redColor] CGColor];
textField.layer.cornerRadius = 5;
textField.clipsToBounds      = YES;

For SWIFT:

textField.layer.borderColor = UIColor.redColor().CGColor;
Nazir
  • 1,945
  • 24
  • 27
5

Use Quartzcore framework. You can set the textField.layer.borderWidth, as well as borderColor:

tField.layer.borderColor = [UIColor redColor].CGColor;

for more reference: https://stackoverflow.com/a/5749376/1554632

Community
  • 1
  • 1
Nikita P
  • 4,226
  • 5
  • 31
  • 55