Search code examples
iosobjective-cuitextviewcopy-paste

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


I am trying to copy a number into UITextView from iPhone Notes app.

screenshot: number copied from Notes app

But after copying and pasting it in my app's textview its adding a prefix (tel:). like : tel:2234356778876

I don't want to show tel: in my UITextView ,just the number. I added checking in textview delegate shouldChangeTextInRange method.

- (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;
}

But its not changing anything. textview showing full tel:22xxxxxxxx number. What should be changed in this case?

Note: If I copy number from Contacts or Safari its working, only number is pasted. But tel: is added for Notes app.

Thanks.


Solution

  • text argument exists in a context of that delegate function, mutating it won't reflect the result in your UITextView.

    What you have to do is to check if string contains "tel:", then manually update your UITextView contents and return NO from that function.

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