drawTextInRect on UITextField not called

让人想犯罪 __ 提交于 2019-11-26 23:15:35

问题


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.


回答1:


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.




回答2:


I guess the method you are looking for is:

- (CGRect)textRectForBounds:(CGRect)bounds

or possibly

- (CGRect)editingRectForBounds:(CGRect)bounds



回答3:


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




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/1920783/drawtextinrect-on-uitextfield-not-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!