27

How can I set the border style of a UITextField programatically?

I am creating my text field like so:

UITextField *tfText = [[UITextField alloc] initWithFrame:CGRectMake(65, 200, 200, 30)];
tfText.backgroundColor = [UIColor colorWithRed:0.2 green:0.9 blue:0.5 alpha:0.3];       
tfText.textAlignment = UITextAlignmentCenter;
[self.view addSubview:tfText];
[tfText release];
Undo
  • 25,519
  • 37
  • 106
  • 129
Kiran
  • 1,289
  • 3
  • 15
  • 25

6 Answers6

51

Try this

UITextField *tfText = [[UITextField alloc] initWithFrame:CGRectMake(65, 200, 200, 30)];
tfText.backgroundColor = [UIColor colorWithRed:0.2 green:0.9 blue:0.5 alpha:0.3];       
tfText.textAlignment = UITextAlignmentCenter;
// Border Style None
[tfText setBorderStyle:UITextBorderStyleNone];
[self.view addSubview:tfText];
[tfText release];

For Reference

pkamb
  • 33,281
  • 23
  • 160
  • 191
Chetan Bhalara
  • 10,326
  • 6
  • 32
  • 51
8

You can use Quartzcore and the layer properties.

sometextfield.layer.borderWidth = 1;
sometextfield.layer.borderColor = [[UIColor redColor] CGColor];

I must remember to add QuartzCore to your project and to import it where you want to use it.

pasine
  • 11,311
  • 10
  • 49
  • 81
6

From below four, anyone can be used programatically,

[textField setBorderStyle:UITextBorderStyleNone];

[textField setBorderStyle:UITextBorderStyleLine];

[textField setBorderStyle:UITextBorderStyleBezel];

[textField setBorderStyle:UITextBorderStyleRoundedRect];

where textField is an outlet of UITextField

iAnkit
  • 1,940
  • 19
  • 25
4

I know, the question is about Obj-C, but i came here for Swift. And in Swift, it's done like that:

txt.backgroundColor = UIColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 1.0);
txt.borderStyle = UITextBorderStyle.RoundedRect
let myColor : UIColor = UIColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 1.0);
txt.layer.borderWidth = 3;
txt.layer.borderColor = myColor.CGColor;

This sets a custom background color and a border in the same color (to hide the border but still have some padding between text and the textfield borders.

dy_
  • 2,433
  • 24
  • 27
2
[pTxtTithiTime.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];

[pTxtTithiTime.layer setBorderWidth:0.8];

//The rounded corner part, where you specify your view's corner radius:

  pTxtTithiTime.layer.cornerRadius = 5;

  pTxtTithiTime.clipsToBounds = YES;
Olter
  • 1,129
  • 1
  • 21
  • 40
Ela
  • 59
  • 2
0

You can make UITextField Programmatically like this:

UITextField *txtField = [[UITextField alloc]initWithFrame:CGRectMake(10, 260, 280, 30)]; // your required coordinate
txtField.delegate =        self;
txtField.placeholder =     @"My TextField";
txtField.borderStyle =      UITextBorderStyleNone;
txtField.keyboardType =        UIKeyboardTypeDefault;
txtField.backgroundColor =       [self setBackGroundColorOnButton];
txtField.layer.cornerRadius = 5.0;
bjb568
  • 11,089
  • 11
  • 50
  • 71
Programming Learner
  • 4,351
  • 3
  • 22
  • 34