2

I'm almost sure this question has been asked before, but I searched for almost an hour and didn't find anything

This is my code:

textField.font = UIFont.systemFont(ofSize: 12.0)
textField.layer.borderWidth = 0.9
textField.placeholder = "border is 0.9 clip is false"
textField.layer.borderColor = UIColor.black.cgColor
textField.borderStyle = UITextBorderStyle.roundedRect
textField.returnKeyType = UIReturnKeyType.done

enter image description here

Doesn't matter if set textField.clipsToBounds to true or false, the borders won't be rounded. However if I change the background color, it will fill it as it has a rounded border, see the image below.

enter image description here

if you zoom in, you'll see that the it doesn't fill the corners completely

If I set textField.layer.borderWidth = 0.1 then I would have a nice roundedRect, but they border would then have to be a thin line. See the image below: enter image description here

again setting clipToBounds = true won't make any difference.

EDIT:

So the answer is to do textField.layer.cornerRadius = 5. But then I wonder what is the UITextBorderStyle.roundedRect doing then?!

mfaani
  • 33,269
  • 19
  • 164
  • 293
  • 2
    To make borders rounded use cornerRadius property `textField.layer.cornerRadius = 5`. I think you misunderstood clipToBounds property. See more [examples](http://stackoverflow.com/questions/1824463/how-to-style-uitextview-to-like-rounded-rect-text-field) – Jurasic Mar 22 '17 at 14:03
  • @Jurasic that was it! so what is does the `UITextBorderStyle.roundedRect` doing?!! – mfaani Mar 22 '17 at 14:17

1 Answers1

3

Try textField.layer.cornerRadius = 5

cornerRadius - the radius to use when drawing rounded corners for the layer’s background. Animatable.

var cornerRadius: CGFloat { get set }

Setting the radius to a value greater than 0.0 causes the layer to begin drawing rounded corners on its background. By default, the corner radius does not apply to the image in the layer’s contents property; it applies only to the background color and border of the layer. However, setting the masksToBounds property to true causes the content to be clipped to the rounded corners.

The default value of this property is 0.0.

Update:

But then I wonder what is the UITextBorderStyle.roundedRect doing then?

Basically the same thing but with default radius. Maybe it was overwritten somewhere. Check out your IB properties, in Attribute Inspector, Border Style property:

enter image description here

Jurasic
  • 1,845
  • 1
  • 23
  • 29