Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a prefix to UITextField

I want a '$' Sign in the text field which cannot be erased. A user should be able to enter values after it and if he presses backspace it should only erase the value he entered.

The reason I can't use UILabel before it is because the text in UITextfield is center aligned and it grows to either side as the user enters values.

Please provide any solution.

like image 214
carbonr Avatar asked Sep 11 '25 10:09

carbonr


1 Answers

  1. Set the text field's text to "$" initially.
  2. Set the text field's delegate to some object of yours (probably the view controller containing it).
  3. Implement -textField:shouldChangeCharactersInRange:replacementString: and return NO if the proposed change would delete the "$".

Also, you might need to implement -textFieldDidBeginEditing: to position the cursor after the "$" if it's not there already.

like image 187
rickster Avatar answered Sep 13 '25 00:09

rickster