-1

I have a UITextView. I have the delegate for myTextView set to self and, when I do normal editing, this method calls just fine:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSLog(@"Called");
}

In my app, I call in my code: [myTextView insertText:@"Hello World"];. When I do, I need to call textView:shouldChangeTextInRange:replacementText: after the text is inserted. How do I do this?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Cody Winton
  • 2,989
  • 5
  • 25
  • 48

2 Answers2

4

Call it explicitly. Call it before editing and only perform the edit if it returns YES. To call it explicitly you need to know the selected range (get the selected range with selectedTextRange), that's it. You already have the text to add and the text view.

Wain
  • 118,658
  • 15
  • 128
  • 151
2

Thanks for the answer, @Wain!

Here's what worked:

- (void)insertText
{
    NSString *stringToAdd = @"Hello World";

    NSString *replacementText = [myTextView.text stringByAppendingString:stringToAdd];

    [napkinTextView insertText:stringToAdd];

    [self textView:myTextView shouldChangeTextInRange:NSMakeRange(0, stringToAdd.length) replacementText:replacementText];
}
Cody Winton
  • 2,989
  • 5
  • 25
  • 48