drawTextInRect on UITextField not called

前端 未结 5 2003
不知归路
不知归路 2020-12-06 06:56

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]

相关标签:
5条回答
  • 2020-12-06 07:18

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

    0 讨论(0)
  • 2020-12-06 07:27

    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.

    0 讨论(0)
  • 2020-12-06 07:29

    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.

    0 讨论(0)
  • 2020-12-06 07:38

    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.

    0 讨论(0)
  • 2020-12-06 07:38

    I guess the method you are looking for is:

    - (CGRect)textRectForBounds:(CGRect)bounds
    

    or possibly

    - (CGRect)editingRectForBounds:(CGRect)bounds
    
    0 讨论(0)
提交回复
热议问题