I have a UITextField
that I'm forcing formatting on by modifying the text inside the change notification handler. This works great (once I solved the reentrancy issues) but leaves me with one more nagging problem.
If the user moves the cursor someplace other than the end of the string then my formatting change moves it to the end of the string. This means users cannot insert more than one character at a time into the middle of the text field.
Is there a way to remember and then reset the cursor position in the UITextField
?
Ios – Control cursor position in UITextField
iosiphoneuitextfielduitextpositionuitextrange
Related Topic
- Ios – iPhone UITextField – Change placeholder text color
- Ios – Text inset for UITextField
- Ios – Set padding for UITextField with UITextBorderStyleNone
- IOS – Dismiss keyboard when touching outside of UITextField
- Ios – Prevent user from setting cursor position on UITextField
- Ios – Getting the cursor position of UITextField in ios
- Ios – Format UITextField text without having cursor move to the end
Best Answer
Controlling cursor position in a UITextField is complicated because so many abstractions are involved with input boxes and calculating positions. However, it's certainly possible. You can use the member function
setSelectedTextRange
:Here's a function which takes a range and selects the texts in that range. If you just want to place the cursor at a certain index, just use a range with length 0:
For example, to place the cursor at
idx
in the UITextFieldinput
: