简体   繁体   中英

Disable wrong phone number link detection UITextView Swift

My textView detects a number "123456789" as a phone number. The automatic link detection changes its color and highlights it when the user taps it.

The thing is that I want to disable the automatic link detection of that textView for just that number .

I have tried to remove the phoneNumber as a detectorType but in that case the textView does not recognize any phone number in the textView.

Does someone know what can I do to approach this?

You can disable autodetection for whole textview and add links to needed numbers. And you should know them exactly to easy find them or at least their 'good' pattern, to use regex to find matches and set links

I guess that phone numbers use URL scheme tel like tel:1-408-555-5555 for example.

let phoneRange = textView.text.range(of: "1-408-555-5555")
let phoneURL = URL(string: "tel:1-408-555-5555")!
let linkString = NSMutableAttributedString(string: textView.text)
linkString.addAttribute(.link, value: phoneURL, range: phoneRange)
textView.attributedText = linkString

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