Iphone 如何设置UITextfield的边框样式

Iphone 如何设置UITextfield的边框样式,iphone,ios,objective-c,uitextfield,Iphone,Ios,Objective C,Uitextfield,如何以编程方式设置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 = UITextAl

如何以编程方式设置
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];

可以使用Quartzcore和图层特性

sometextfield.layer.borderWidth = 1;
sometextfield.layer.borderColor = [[UIColor redColor] CGColor];
我必须记住将QuartzCore添加到您的项目中,并将其导入到您想要使用它的地方。

试试这个

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];
供参考

[pTxtTithiTime.layer setBorderColor:[[UIColor grayColor]colorWithAlphaComponent:0.5]CGColor];
[pTxtTithiTime.layer setBorderWidth:0.8];
//圆角部分,指定视图的角半径:

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

您可以通过编程方式使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;
从四级以下开始,任何人都可以编程使用


其中textFieldUITextField

的出口我知道,问题是关于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;
这将设置自定义背景色和相同颜色的边框(以隐藏边框,但在文本和文本字段边框之间仍有一些填充)

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;