简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM