ios 如何设置 UITextField 的 borderColor?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24764596/
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
How do I set a UITextField's borderColor?
提问by Narayan Srivathsan
I am trying to set a UITextField
's borderColor
in iOS 8 in Swift. I tried using this code:
我想设置UITextField
的borderColor
斯威夫特在IOS 8。我尝试使用此代码:
myTextField.layer.borderColor = UIColor( red: 0.5, green: 0.5, blue:0, alpha: 1.0 )
I got this error:
我收到此错误:
UIColor not convertible to CGColor
UIColor 不能转换为 CGColor
回答by weePee
@coderRed gave you an Objective C answer and a Swift answer. Ignore his first Line
@coderRed 给了你一个 Objective C 的答案和一个 Swift 的答案。忽略他的第一行
I broke it up into 2 statements below in Swift:
我在 Swift 中将其分解为以下 2 个语句:
let myColor : UIColor = UIColor( red: 0.5, green: 0.5, blue:0, alpha: 1.0 )
myTextField.layer.borderColor = myColor.CGColor
Hope this helps! Good luck with learning Swift
希望这可以帮助!祝学习 Swift 好运
回答by Diamond King
You can try this,it works for me
你可以试试这个,它对我有用
myTextField.layer.cornerRadius = 8.0
myTextField.layer.masksToBounds = true
myTextField.layer.borderColor = UIColor( red: 153/255, green: 153/255, blue:0/255, alpha: 1.0 ).CGColor
myTextField.layer.borderWidth = 2.0
回答by HymansonHaenchen
Try this:
尝试这个:
myTextField.layer.borderColor = [UIColor blackColor].CGColor;
myTextField.layer.borderColor = (UIColor( red: 0.5, green: 0.5, blue:0, alpha: 1.0 )).CGColor;
回答by Bob
I believe you need to set the borderWidth
我相信你需要设置borderWidth
myTextField.layer.borderWidth = 10
回答by JustAnotherGuy
I found that the simplest way to set the border color using Swift 3 is to do
我发现使用 Swift 3 设置边框颜色的最简单方法是
myTextField.layer.borderColor = UIColor.gray.cgColor
or
或者
myTextView.layer.borderColor = UIColor.gray.cgColor
Hope this works! Good luck.
希望这有效!祝你好运。
回答by TimWhiting
The issue with this is about converting a UIColor to a CGColor, which is explained in the above answers, but put most simply, all you have to do to do this conversion is add .CGColor
to the end of your UIColor. i.e.
这个问题是关于将 UIColor 转换为 CGColor,这在上面的答案中进行了解释,但最简单地说,进行此转换所需要做的就是添加.CGColor
到 UIColor 的末尾。IE
myTextField.layer.borderColor = UIColor( red: CGFloat, green: CGFloat, blue:CGFLoat, alpha: CGFloat ).CGColor
回答by Milan Sanathara
myTextField.layer.borderColor = UIColor.darkGrayColor().CGColor
回答by Joris416
Your code:
您的代码:
myTextField.layer.borderColor = [UIColor blackColor].CGColor;
myTextField.layer.borderColor = (UIColor( red: 0.5, green: 0.5, blue:0, alpha: 1.0 )).CGColor;
What I do in swift and what ALWAYS works:
我在 swift 中所做的以及始终有效的操作:
myTextField.layer.borderColor = UIColor.blackColor().CGColor;
myTextField.layer.borderColor = UIColor(red: 0.5, green: 0.5, blue:0, alpha: 1.0 ).CGColor;
Hope this helps :)
希望这可以帮助 :)
回答by mark_dimitry
You might also want to add line like:
您可能还想添加如下行:
myTextField.layer.borderWidth = CGFloat(1.0)
To previous answers.
对于以前的答案。
In my case, color change wasn't visible, until I placed this line.
就我而言,颜色变化是不可见的,直到我放置了这条线。
回答by Judit
Try this,
尝试这个,
extension UITextField{
func setBorderColor(width:CGFloat,color:UIColor) -> Void{
self.layer.borderColor = color.cgColor
self.layer.borderWidth = width
}
}
and implement this function wherever you want,
并在任何你想要的地方实现这个功能,
textField.setBorderColor(width: 2.0, color: UIColor.brown)
Hope this help!
希望这有帮助!