I want to be able to replace some text in an UITextView programatically, so I wrote this method as an UITextView category:
- (void) replaceCharactersInRange:(NSR
After having stumbled over this issue and getting grey hair of it, I finally found a satisfying solution. It's a bit tacky, but it DOES work like a charm. The idea is to use the working copy&paste support that UITextView offers! I think you might be interested:
- (void)insertText:(NSString *)insert
{
UIPasteboard *pboard = [UIPasteboard generalPasteboard];
// Clear the current pasteboard
NSArray *oldItems = [pboard.items copy];
pboard.items = nil;
// Set the new content to copy
pboard.string = insert;
// Paste
[self paste: nil];
// Restore pasteboard
pboard.items = oldItems;
[oldItems release];
}
EDIT: Of course you can customize this code to insert text at any position in the text view. Just set the selectedRange
before calling paste
.