I use the following code to simulate a key press in a UITextView (and also to set the scroll to the insertion position):
NSRange selectedRange = textview.selectedRange;
NSString *currentText = textview.text;
NSString *yourString = @"x";
NSString *firstPart = [currentText substringToIndex: selectedRange.location];
NSString *lastPart = [currentText substringFromIndex: selectedRange.location];
NSString *modifiedText = [firstPart stringByAppendingFormat:@"%@%@", yourString, lastPart];
textview.text = modifiedText;
NSInteger loc = selectedRange.location + 1;
NSInteger len = textview.selectedRange.length;
NSRange newRange = NSMakeRange(loc, len);
textview.selectedRange = newRange;
As you can see, i divide the textview.text, i insert the @”x” in the cursor position and i modify the whole text. This works great, as a matter of fact, unless the length of the text file is big. And this sounds logical, given that i divide the whole thing in parts and so with every simulated key.
So with a small text file i have no problem whatsoever, but with a big one i can see a considerable lag.
Is there any way to do this with a better performance?
Instead of rewriting the whole text at each keystroke, you should use the
-(void)insertText:(NSString*)textmethodSample code from here: