Luchi Parejo Alcazar
Luchi Parejo Alcazar

Reputation: 151

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?

Upvotes: 0

Views: 635

Answers (1)

Andrey Kuznetsov
Andrey Kuznetsov

Reputation: 843

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

Upvotes: 1

Related Questions