【问题标题】:UITextView insertText call shouldChangeTextInRange: replacementText:UITextView insertText 调用 shouldChangeTextInRange: replacementText:
【发布时间】:2013-09-25 19:03:28
【问题描述】:

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

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

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

【问题讨论】:

  • 您自然会在进行编辑之前显式调用它,然后在返回YES时进行编辑。
  • @Wain - 我该如何明确称呼它?

标签: ios objective-c uitextview uitextviewdelegate


【解决方案1】:

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

【讨论】:

    【解决方案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];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-17
      • 1970-01-01
      • 2012-03-06
      • 1970-01-01
      • 2018-07-04
      相关资源
      最近更新 更多