开发者

how to set border style of a UITextfield

开发者 https://www.devze.com 2023-02-28 23:28 出处:网络
How can I set the border style of a UITextField programatically? I am creating my text field like so: UITextField *tfText 开发者_StackOverflow= [[UITextField alloc] initWithFrame:CGRectMake(65, 200,

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

I am creating my text field like so:

UITextField *tfText 开发者_StackOverflow= [[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];


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

  • http://www.icodeblog.com/2010/01/04/uitextfield-a-complete-api-overview/


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.


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


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.


[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;


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消