9

I'm trying to implement the answer to this SO question. The problem is: -[drawTextInRect] is apparently not called, and setting the shadow in -[drawRect] doesn't make the UITextField's text shadowed.

Another weird thing is that even if my subclass implementations of -[drawTextInRect] and -[drawRect] are completely empty (not even a call to super), the textfield's text is drawn.

Community
  • 1
  • 1
mrueg
  • 8,185
  • 4
  • 44
  • 66

5 Answers5

12

This is a bug in the UITextField API documentation. The documentation indicates that overriding drawTextInRect: can be used to customize behaviour. This is not the case.

In fact, drawTextInRect: will never be called on an UITextField (drawPlaceholderInRect: will be called neither by the way).

See also http://discussions.apple.com/thread.jspa?threadID=1727596.

Overriding the method on UILabel works though.

henning77
  • 3,824
  • 2
  • 26
  • 32
  • Correction: `drawPlaceholderInRect:` is still called. I use it in my subclasses to alter the font and text color of the placeholder. – marcshilling May 13 '15 at 15:31
4

I guess the method you are looking for is:

- (CGRect)textRectForBounds:(CGRect)bounds

or possibly

- (CGRect)editingRectForBounds:(CGRect)bounds
cescofry
  • 3,706
  • 3
  • 26
  • 22
3

I note that -[drawTextInRect] is called once when the UITextField loses focus. Not what I wanted.

PKCLsoft
  • 1,359
  • 2
  • 26
  • 35
0

UITextField does not respond to a - drawTextInRect: message. The code on the page you reference subclasses a UILabel not a UITextField, which is why it didn't work for you.

greg
  • 4,843
  • 32
  • 47
-1

If you don't call super, the compiler might be removing the method during optimization of your code. Effectively meaning that UILabel's implementation is getting called.


Update: If you are using a nib to create the textfield, be sure you have set the class of the textfield in the nib to your custom subclass, otherwise your custom code will not be called.

Corey Floyd
  • 25,929
  • 31
  • 126
  • 154
  • Just reading your last line and letting you know why that wouldn't work for testing purposes: Another weird thing is that even if my subclass implementations of -[drawTextInRect] and -[drawRect] are completely empty (not even a call to super), the textfield's text is drawn. – Corey Floyd Jan 08 '10 at 17:31
  • if you don't call super it's more like overriding a method and providing a custom implementation - a method does nothing in such case altering the default behavior. There is no reason for compiler to "optimize" a code by removing an empty methods implementation in subclasses for such cases. Disclaimer: I didn't vote down :) – vir us Apr 08 '16 at 23:32