UITextField 边框颜色
我非常希望将自己的颜色设置为 UITextField 边框。但到目前为止我只能找到如何更改边框线样式。
我已经使用背景属性以这种方式设置背景颜色:
self.textField.backgroundColor = textFieldColor;
但我也必须更改 UITextField 边框的颜色。我的问题是如何更改边框颜色。
I have really great wish to set my own color to UITextField border. But so far I could find out how to change the border line style only.
I've used background property to set background color in such way:
self.textField.backgroundColor = textFieldColor;
But I have to change color of UITextField border too. And my question was about how to change the border color.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
在类中导入 QuartzCore 框架:
要更改边框颜色,请使用以下代码片段(我将其设置为 redColor),
要恢复到原始布局,只需将边框颜色设置为透明颜色,
在快速代码中
Import
QuartzCore
framework in you class:and for changing the border color use the following code snippet (I'm setting it to redColor),
For reverting back to the original layout just set border color to clear color,
in swift code
试试这个:
并导入 QuartzCore:
Try this:
and import QuartzCore:
更新为 swift 5.0
Update for swift 5.0
导入以下类:
//用于设置文本字段边框灰色的代码
根据需要将
171.0
替换为相应的颜色编号。Import the following class:
//Code for setting the grey color for the border of the text field
Replace
171.0
with the respective color number as required.这个问题在谷歌搜索中出现得相当高,而且大部分都有效!我确实发现 Salman Zaidi 的答案对于 iOS 7 来说是部分正确的。
您需要对“恢复”代码进行修改。我发现以下恢复效果非常好:
我知道这很可能是由于 iOS 7 中的更改所致。
this question shows up pretty high on a Google search and worked for the most part! I did find that Salman Zaidi's answer was partially correct for iOS 7.
You need to make a modification to the "reverting" code. I found that the following for reverting worked perfectly:
I understand that this is most likely due to changes in iOS 7.
为了简化接受答案的操作,您还可以为
UIView
创建 Category (因为这适用于 UIView 的所有子类,而不仅仅是文本字段:UIView+Additions .h:
UIView+Additions.m:
用法:
To simplify this actions from accepted answer, you can also create Category for
UIView
(since this works for all subclasses of UIView, not only for textfields:UIView+Additions.h:
UIView+Additions.m:
Usage:
如果您使用带圆角的 TextField,请使用以下代码:
要删除边框:
If you use a TextField with rounded corners use this code:
To remove the border:
任何视图(或 UIView 子类)上的 borderColor 也可以使用情节提要和一些编码来设置,如果您要在多个 UI 对象上设置边框颜色,这种方法可能非常方便。
以下是如何实现它的步骤,
PS:请记住,类别不能存储属性。 “borderUIColor”用作计算属性,只是作为实现我们关注的目标的参考。
请看下面的代码示例;
目标 C:
接口文件:
实现文件:
Swift 2.0:
最后转到故事板/XIB,按照其余步骤;
您必须将 layer.borderWidth 属性值设置为至少 1 才能看到边框颜色。
构建并运行。
快乐编码。 :)
borderColor on any view(or UIView Subclass) could also be set using storyboard with a little bit of coding and this approach could be really handy if you're setting border color on multiple UI Objects.
Below are the steps how to achieve it,
P.S: Remember, Categories can't have stored properties. 'borderUIColor' is used as a calculated property, just as a reference to achieve what we're focusing on.
Please have a look at the below code sample;
Objective C:
Interface File:
Implementation File:
Swift 2.0:
And finally go to your storyboard/XIB, follow the remaining steps;
You've to set layer.borderWidth property value to at least 1 to see the border color.
Build and Run.
Happy Coding. :)
像这样称呼它:
email.addBorder(1.0,颜色:.blue,cornerRadius:5)
。Call this like:
email.addBorder(1.0, color: .blue, cornerRadius: 5)
.