Add a prefix to UITextField

后端 未结 4 1426
故里飘歌
故里飘歌 2021-01-22 02:18

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 entere

4条回答
  •  野的像风
    2021-01-22 02:35

    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.

提交回复
热议问题