如何以编程方式设置UITextField
的边框样式?
我正在创建我的文本字段:
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];
答案 0 :(得分:51)
试试这个
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];
供参考
答案 1 :(得分:8)
您可以使用Quartzcore和图层属性。
sometextfield.layer.borderWidth = 1;
sometextfield.layer.borderColor = [[UIColor redColor] CGColor];
我必须记住将QuartzCore添加到您的项目中并将其导入您想要使用它的位置。
答案 2 :(得分:5)
从四岁开始,任何人都可以通过编程方式使用,
[textField setBorderStyle:UITextBorderStyleNone];
[textField setBorderStyle:UITextBorderStyleLine];
[textField setBorderStyle:UITextBorderStyleBezel];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
其中 textField 是 TextField 的出口
答案 3 :(得分:2)
[pTxtTithiTime.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[pTxtTithiTime.layer setBorderWidth:0.8];
//圆角部分,您可以在其中指定视角的半径:
pTxtTithiTime.layer.cornerRadius = 5;
pTxtTithiTime.clipsToBounds = YES;
答案 4 :(得分:2)
我知道,问题是关于Obj-C,但是我来到斯威夫特。在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;
这会设置自定义背景颜色和相同颜色的边框(隐藏边框,但文本和文本字段边框之间仍有一些填充。
答案 5 :(得分:0)
您可以像这样以编程方式制作UITextField:
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;