如何设置UITextfield的边框样式

如何以编程方式设置UITextField的边框样式?

我正在创build我的文本字段,如下所示:

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

尝试这个

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

以供参考

你可以使用Quartzcore和图层属性。

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

我必须记得将QuartzCore添加到您的项目,并将其导入到您想要使用它的地方。

从四点以下,任何人都可以用编程方式,

 [textField setBorderStyle:UITextBorderStyleNone]; [textField setBorderStyle:UITextBorderStyleLine]; [textField setBorderStyle:UITextBorderStyleBezel]; [textField setBorderStyle:UITextBorderStyleRoundedRect]; 

其中textFieldUITextField的出口

我知道,问题是关于Obj-C,但是我来到这里是为了Swift。 而在Swift中,就是这样做的:

 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; 

这将自定义背景颜色和边框设置为相同颜色(以隐藏边框,但在文本和文本框边框之间仍然有一些填充。

 [pTxtTithiTime.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]]; [pTxtTithiTime.layer setBorderWidth:0.8]; 

//圆angular部分,指定视图的angular半径:

  pTxtTithiTime.layer.cornerRadius = 5; pTxtTithiTime.clipsToBounds = YES; 

你可以像这样以编程方式创buildUITextField:

 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;