iphoneiosobjective-cuitextfielduitextrange

Insert space in current cursor location


I've searched stackoverflow and the web, but couldn't find something that works. I have a UIButton that inserts a space when you click on it, now it adds the space at the end of the UITextField even when the cursor is in the middle.

How can I add space at current cursor position with UIButton click?

Thanks in advance.


Solution

  • For iOS 5+, try

    [textField replaceRange:[textField selectedTextRange] withText:@" "];