0

I was using this code scope previously:

override func textRect(forBounds bounds: CGRect) -> CGRect {
    super.textRect(forBounds: bounds)
    return UIEdgeInsetsInsetRect(bounds, UIEdgeInsets(top: 0,left: 10, bottom: 0, right: 10))
}

But it gives an error after Xcode, Swift Update. Here is an error:

'UIEdgeInsetsInsetRect' has been replaced by instance method 'CGRect.inset(by:)'

And then i wrote this:

override func textRect(forBounds bounds: CGRect) -> CGRect {
    super.textRect(forBounds: bounds)
    var animationRect = frame.inset(by: UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 10))
    return animationRect
}

But the above code doesn't affect into the text field. What should I write to give a padding from left?

Cœur
  • 37,241
  • 25
  • 195
  • 267
mannyCalavera
  • 593
  • 1
  • 4
  • 23
  • Have you tried bounds.inset ? – Andrew21111 Sep 21 '18 at 10:21
  • Andrea can u write me exactly textRect method with using bounds.inset Thanks – mannyCalavera Sep 21 '18 at 10:28
  • I meant just change frame.inset(by: UIEdgeInsets( ecc.. to bound.inset, but I just tried on a playground and it didn't work, did you try as well ? – Andrew21111 Sep 21 '18 at 10:47
  • Actually, i wrote this code in my question post.. var animationRect = frame.inset(by: UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 10)) It doesn't effect.. – mannyCalavera Sep 21 '18 at 10:49
  • @AndreaAbbate any news? It doesn't effect: var animationRect = frame.inset(by: UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 10)) – mannyCalavera Sep 21 '18 at 11:10
  • 1
    try return this : override func textRect(forBounds bounds: CGRect) -> CGRect { return bounds.inset(by: UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 10)) } I just tried on a MyTextField custom class into storyboard and it does seem to work – Andrew21111 Sep 21 '18 at 11:20
  • Dear @AndreaAbbate, I tried your solution for placeholderRect and editingRect. It works. Thanks a lot. But I have a question: What purpose should we use a TexRect method? Because I thought editingRect was a textRect. Now I see that i was looking for editingRect. Now i have no idea purpose of textRect – mannyCalavera Sep 21 '18 at 11:30
  • 1
    As you can see here [textRect documentation](https://developer.apple.com/documentation/uikit/uitextfield/1619636-textrect) The editingRect returns the rectangle in which editable text can be displayed, instead of this, textRect return the computed drawing rect. Check the previous link, it will be more exhaustive. I'll turn this to a reply, so anyone can see it. – Andrew21111 Sep 21 '18 at 11:35
  • Great. If there is a default text in textfied. This textRect sets this text's padding. Thanks a lot ! – mannyCalavera Sep 21 '18 at 11:39

1 Answers1

1

return this:

override func textRect(forBounds bounds: CGRect) -> CGRect { 
   return bounds.inset(by: UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 10)) 
} 

I just tried on a MyTextField custom class into storyboard and it does seem to work.

To ask your question into comments:

As you can see here textRect documentation The editingRect returns the rectangle in which editable text can be displayed, instead of this, textRect return the computed drawing rect. Check the previous link, it will be more exhaustive.

Andrew21111
  • 868
  • 8
  • 17