Custom UITextField drawing while editing

I am trying to build a textbox with a custom prefix (think of a textbox to enter a phone number and the prefix is ​​the international dialing code). I would like the textbox to be very similar to a regular textbox and an unselectable prefix area. Moreover, I would like the prefix to move and be next to the text when entering a phone number. At the beginning, the prefix should simply be drawn from the placeholder text.

To achieve this, I subclass UITextField and override drawTextInRect and drawPlaceHolderTextInRect to draw a custom prefix in front of the text to be drawn. This works like a charm when the textbox is empty (drawPlaceHolderTextInRect is called) or when the textbox is not in edit mode (drawTextInRect is called). However, when the text box is in edit mode and not empty, none of these methods are called. Is there any other method that I can override to provide a custom drawing when the user enters a phone number in the text box?


source to share

All Articles