更改文本字段边框颜色 iOS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15152564/
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 22:30:23  来源:igfitidea点击:

Changing textField border color iOS

iosobjective-cuitextfieldxib

提问by user2017285

I want to change UITextField bordercolor. Is it possible to customize the color of border? I searched all options in xibbut I did not found any option to change border color.

我想更改UITextField 边框颜色。是否可以自定义边框的颜色?我搜索了所有选项,xib但没有找到任何更改选项border color

回答by KDeogharkar

you can use this:

你可以使用这个:

yourTextField.layer.borderColor=[[UIColor blackColor]CGColor];

yourTextField.layer.borderWidth=1.0;

and remember to import #import <QuartzCore/QuartzCore.h>in your .h file

并记得#import <QuartzCore/QuartzCore.h>在您的 .h 文件中导入

You can also specify your RGB value.

您还可以指定您的 RGB 值。

yourTextField.layer.borderColor=[[UIColor colorWithRed:178.0f/255.0f green:178.0f/255.0f blue:178.0f/255.0f alpha:1.0] CGColor];

Note: you need to set both values starting with iOS 7

注意:您需要从 iOS 7 开始设置这两个值

Update for swift 2.3

更新 swift 2.3

yourTextField.layer.borderColor = UIColor.blackColor().CGColor
yourTextField.layer.borderWidth = 1.0

OR

或者

yourTextField.layer.borderColor = UIColor(red: 178.0 / 255.0, green: 178.0 / 255.0, blue: 178.0 / 255.0, alpha: 1.0).CGColor

Update for swift 3.1.1

Swift 3.1.1 的更新

yourTextField.layer.borderColor = UIColor.black.cgColor
yourTextField.layer.borderWidth = 1.0

OR

或者

yourTextField.layer.borderColor = UIColor(red: 178.0 / 255.0, green: 178.0 / 255.0, blue: 178.0 / 255.0, alpha: 1.0).cgColor

回答by Nazir

#import <QuartzCore/QuartzCore.h>

Use below code to change Textfield's border color

使用以下代码更改文本字段的边框颜色

textField.layer.borderWidth = 2.0f;
textField.layer.borderColor = [[UIColor redColor] CGColor];
textField.layer.cornerRadius = 5;
textField.clipsToBounds      = YES;

For SWIFT:

对于 SWIFT:

textField.layer.borderColor = UIColor.redColor().CGColor;

回答by Nikita P

Use Quartzcore framework. You can set the textField.layer.borderWidth, as well as borderColor:

使用 Quartzcore 框架。您可以设置 textField.layer.borderWidth 以及 borderColor:

tField.layer.borderColor = [UIColor redColor].CGColor;

for more reference: https://stackoverflow.com/a/5749376/1554632

更多参考:https: //stackoverflow.com/a/5749376/1554632