禁用错误 phone 数字 link 检测 UITextView Swift
Disable wrong phone number link detection UITextView Swift
我的 textView 将数字“123456789”检测为 phone 数字。自动 link 检测会更改其颜色并在用户点击时突出显示。
问题是我想禁用 那个 textView 的自动 link 检测只是那个数字 。
我试图删除 phoneNumber 作为 detectorType,但在这种情况下,textView 无法识别 textView 中的任何 phone 数字。
有人知道我该怎么做才能解决这个问题吗?
您可以禁用整个文本视图的自动检测并添加指向所需数字的链接。您应该确切地了解它们以便轻松找到它们或至少它们的 'good' 模式,以使用正则表达式查找匹配项并设置链接
我猜 phone 数字使用 URL 方案 tel
例如 tel:1-408-555-5555
。
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
我的 textView 将数字“123456789”检测为 phone 数字。自动 link 检测会更改其颜色并在用户点击时突出显示。
问题是我想禁用 那个 textView 的自动 link 检测只是那个数字 。
我试图删除 phoneNumber 作为 detectorType,但在这种情况下,textView 无法识别 textView 中的任何 phone 数字。
有人知道我该怎么做才能解决这个问题吗?
您可以禁用整个文本视图的自动检测并添加指向所需数字的链接。您应该确切地了解它们以便轻松找到它们或至少它们的 'good' 模式,以使用正则表达式查找匹配项并设置链接
我猜 phone 数字使用 URL 方案 tel
例如 tel:1-408-555-5555
。
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