ios 在 Swift 中指定 UITextField 的边框半径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34007920/
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
Specify border radius of UITextField in Swift
提问by Daniel Bramhall
I would like to programmatically specify the border radius of a UITextField
using Swift.
我想以编程方式指定UITextField
使用 Swift的边界半径。
By default, a UITextField
has a slight border radius, however I would like to increase it and, after trawling through Interface Builder, I assume this can be done programmatically?
默认情况下, aUITextField
有一个很小的边界半径,但是我想增加它,并且在浏览 Interface Builder 之后,我认为这可以通过编程来完成?
回答by FredLoh
You can use:
您可以使用:
nameOfTextField.layer.cornerRadius = 15.0
nameOfTextField.layer.borderWidth = 2.0
nameOfTextField.layer.borderColor = UIColor.red.cgColor
回答by Jason Newell
If you're using Interface Builder, you can use User Defined Runtime Attributes for the controls you want to modify. At runtime, when the view loads, each attribute you pass a key path for will automatically be set to your value. No need to clutter up your code with little display tweaks.
如果您使用的是 Interface Builder,则可以对要修改的控件使用用户定义的运行时属性。在运行时,当视图加载时,您传递关键路径的每个属性都将自动设置为您的值。无需通过很少的显示调整来弄乱您的代码。
The picture below should answer your question.
下面的图片应该可以回答你的问题。
回答by Diptee Parmar
To Create a Corner Radius of Textfield below answer is correct:
要在下面的答案中创建文本字段的角半径是正确的:
swift 3
迅捷 3
YourTextfieldName.layer.cornerRadius = YourTextfieldName.frame.size.height/2
YourTextfieldName.clipsToBounds = true
回答by mokagio
As @SnarfSnarf suggests you can programmatically tune the border widht and corner radius of the text field, _and any other view for the matter, by accessing its layer
properties:
正如@SnarfSnarf 建议的那样,您可以通过访问其layer
属性,以编程方式调整文本字段的边框宽度和角半径,_和任何其他视图:
nameOfTextField.layer.cornerRadius = 4.0
nameOfTextField.layer.borderWidth = 2.0
On top of that, UITextField
has a borderStyle
property which you might want to play with. It has four possible values: None
, Line
, Bezel
, and RoundedRect
.
最重要的是,UITextField
有一个borderStyle
你可能想玩的属性。它有四个可能的值:None
,Line
,Bezel
,和RoundedRect
。
Finally have a read through UITextField
's documentation, it's always a good way to find out all the possibility a class provides.
最后通读一下UITextField
的文档,它始终是找出类提供的所有可能性的好方法。
回答by hgcahez
try this :yourTxtField.layer.cornerRadius
You can also find other methods here https://developer.apple.com/library/prerelease/tvos/documentation/UIKit/Reference/UITextField_Class/index.html
试试这个:yourTxtField.layer.cornerRadius
你也可以在这里找到其他方法https://developer.apple.com/library/prerelease/tvos/documentation/UIKit/Reference/UITextField_Class/index.html
回答by Diptee Parmar
To Create borderWidth of Textfield below answer is correct:
要在下面的答案中创建 Textfield 的 borderWidth 是正确的:
swift 3
迅捷 3
YourTextfieldName!.layer.borderWidth = 1
YourTextfieldName!.layer.borderColor = UIColor.black.cgColor
回答by user3561722
Don't forget to add layer.masksToBound - I think the most popular answer missed that. You need the button to have the same form as its' borders. https://developer.apple.com/documentation/quartzcore/calayer/1410896-maskstobounds
不要忘记添加 layer.masksToBound - 我认为最受欢迎的答案忽略了这一点。您需要按钮具有与其边框相同的形式。 https://developer.apple.com/documentation/quartzcore/calayer/1410896-maskstobounds
nameOfTextField.layer.cornerRadius = 15.0
nameOfTextField.layer.borderWidth = 0
nameOfTextField.layer.masksToBounds = true