I would like to programmatically specify the border radius of a UITextField
using 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?
You can use:
nameOfTextField.layer.cornerRadius = 15.0
nameOfTextField.layer.borderWidth = 2.0
nameOfTextField.layer.borderColor = UIColor.red.cgColor
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.
The picture below should answer your question.
To Create a Corner Radius of Textfield below answer is correct:
swift 3
YourTextfieldName.layer.cornerRadius = YourTextfieldName.frame.size.height/2
YourTextfieldName.clipsToBounds = true
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:
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
.
Finally have a read through UITextField
's documentation , it's always a good way to find out all the possibility a class provides.
You can do it with a storyboard too. Just write this extension in your extension file. Then select the text field in the storyboard You will see the option to change the corner radius
extension UIView {
@IBInspectable var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}
试试这个: yourTxtField.layer.cornerRadius
你也可以在这里找到其他方法https://developer.apple.com/library/prerelease/tvos/documentation/UIKit/Reference/UITextField_Class/index.html
This work for me,
nameOfTextField.layer.cornerRadius = 15.0
nameOfTextField.layer.borderWidth = 2.0
nameOfTextField.layer.borderColor = UIColor.brown.cgColor
nameOfTextField.layer.masksToBounds = true
in Swift 5
yourTxtField.cornerRadius = 18
yourTxtField.masksToBounds = true
To Create borderWidth of Textfield below answer is correct:
swift 3
YourTextfieldName!.layer.borderWidth = 1
YourTextfieldName!.layer.borderColor = UIColor.black.cgColor
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
nameOfTextField.layer.cornerRadius = 15.0
nameOfTextField.layer.borderWidth = 0
nameOfTextField.layer.masksToBounds = true
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.