ios 如何设置 UITextfield 的边框样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5789816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:43:12  来源:igfitidea点击:

how to set border style of a UITextfield

iphoneiosobjective-cuitextfield

提问by Kiran

How can I set the border style of a UITextFieldprogramatically?

如何以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];

回答by Chetan Bhalara

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

以供参考

回答by pasine

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

回答by iAnkit

From below four, anyone can be used programatically,

从以下四个,任何人都可以编程使用,

[textField setBorderStyle:UITextBorderStyleNone];

[textField setBorderStyle:UITextBorderStyleLine];

[textField setBorderStyle:UITextBorderStyleBezel];

[textField setBorderStyle:UITextBorderStyleRoundedRect];

where textFieldis an outlet of UITextField

其中textFieldUITextField的出口

回答by datayeah

I know, the question is about Obj-C, but i came here for Swift. And in Swift, it's done like that:

我知道,问题是关于 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;

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.

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

回答by Ela

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

回答by Annu

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;