简体   繁体   English

如何设置 UITextfield 的边框样式

[英]how to set border style of a UITextfield

How can I set the border style of a UITextField programatically?如何以编程方式设置UITextField的边框样式?

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

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以供参考

You can use Quartzcore and the layer properties.您可以使用 Quartzcore 和图层属性。

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.我必须记住将 QuartzCore 添加到您的项目中,并将其导入到您想要使用的地方。

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其中textFieldUITextField的出口

I know, the question is about Obj-C, but i came here for Swift.我知道,问题是关于 Obj-C,但我来这里是为了 Swift。 And in Swift, it's done like that:在 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;

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:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM