3

I am trying to build a textfield with a custom prefix (think of the textfield for inputting a phone number and the prefix being the international dialling code). I would like the textfield to behave very much like a normal textfield and the prefix area to be not selectable. Moreover, I would like the prefix to move and stay close to the text as the phone number is being inputted. At the start, the prefix should just be drawn in from of the placeholder text.

To achieve this, I am subclassing UITextField and overriding the drawTextInRect and drawPlaceHolderTextInRect to draw a custom prefix in front of the text to be drawn. This works like a charm when the textfield is empty (drawPlaceHolderTextInRect gets called) or when the textfield is not in editing mode (drawTextInRect gets called). However, when the textfield is in editing mode and not empty, neither of those methods get called. Is there any other method I can override to provide custom drawing while the user is inputting the phone number in the textfield?

Daniyar
  • 2,975
  • 2
  • 26
  • 39
quentinadam
  • 3,058
  • 2
  • 27
  • 42
  • You might try using `shouldChangeCharactersInRange` method and test if they are trying to delete the first # of characters that you don't want them too. If they are not, allow the new character to be edited/added as normal otherwise ignore user input. – Automate This Sep 17 '14 at 13:57
  • I don't want to go that route because I would like to keep the placeholder text whenever the textfield is empty (but would only contain the prefix), and I would like the prefix to not be selectable. – quentinadam Sep 17 '14 at 14:03
  • could you provide your overrode methods code? – Daniyar Aug 25 '15 at 09:38

0 Answers0