-1

我有一个UITextView. 我将委托myTextView设置为self,当我进行正常编辑时,此方法调用得很好:

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

在我的应用程序中,我调用了我的代码:[myTextView insertText:@"Hello World"];. 当我这样做时,我需要textView:shouldChangeTextInRange:replacementText:在插入文本后调用。我该怎么做呢?

4

2 回答 2

4

明确地调用它。在编辑之前调用它,并且只有在它返回时才执行编辑YES。要明确调用它,您需要知道所选范围(使用 获取所选范围selectedTextRange),就是这样。您已经有了要添加的文本和文本视图。

于 2013-09-25T19:34:08.637 回答
2

感谢您的回答,@Wain!

这是有效的:

- (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];
}
于 2013-09-25T19:34:39.250 回答