粘贴到 UITextView 时如何更改复制的数字?

How can I change copied number when pasting it into UITextView?

我正在尝试从 iPhone Notes 应用程序将数字复制到 UITextView 中。

screenshot: number copied from Notes app

但是在将它复制并粘贴到我的应用程序的文本视图中后,它添加了一个前缀 (tel:)。 喜欢 : 电话:2234356778876

我不想在我的 UITextView 中显示电话号码,只显示电话号码。我在 textview 委托 shouldChangeTextInRange 方法中添加了检查。

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if(textView == destination) // destination is the textview
    {
        if([text hasPrefix:@"tel:"])
        {
            text = [text stringByReplacingOccurrencesOfString:@"tel:" withString:@""];
        }
    }
    return YES;
}

但它没有改变任何东西。显示完整 tel:22xxxxxxxx 数字的文本视图。在这种情况下应该更改什么?

注意:如果我从 ContactsSafari 复制号码,它的工作,只有号码被粘贴。但是 tel: 是为 Notes app.

添加的

谢谢。

text 参数存在于该委托函数的上下文中,改变它不会反映 UITextView.

中的结果

您需要做的是检查字符串是否包含 "tel:",然后从该函数手动更新您的 UITextView 内容和 return NO

if([text hasPrefix:@"tel:"]) {
    NSString *cleanText = [text stringByReplacingOccurrencesOfString:@"tel:" withString:@""];
    textView.text = [textView.text stringByReplacingCharactersInRange:range withString:cleanText];
    return NO;
}