Hi,
I am trying to have multiple hyperlinks within a TextView using dataDetectors and attributedStrings, but I am only able to get one at a time. I am sure that I am missing something, but cannot think of what…
Basically, I can get only one link working at a time.
Here’s the sample code that I am using:
extension NSAttributedString {
static func makeHyoerLink(for path: String, in string: String, as substring: String) → NSAttributedString {
let nsString = NSString(string: string)
let subStringRange = nsString.range(of: substring)
let attributedString = NSMutableAttributedString(string: string)
attributedString.addAttribute(.link, value: path, range: subStringRange)
return attributedString
}
}
Then calling the following method:
private func configureHyperLink(link: String, key: String) → NSAttributedString {
textView.dataDetectorTypes = .all
textView.font = UIFont.preferredFont(forTextStyle: .body)
textView.isEditable = false
textView.isSelectable = true
textView.isUserInteractionEnabled = true
textView.dataDetectorTypes = .link
textView.backgroundColor = .systemBackground
let path = link
let attrivbutedString = NSAttributedString.makeHyoerLink(for: path, in: textView.text, as: key)
return attrivbutedString
}
let font = textView.font
let color = textView.textColor
textView.attributedText = configureHyperLink(link: “https://www.apple.com”, key: “life”)
textView.text = str1 + “Test the news”
textView.font = font
textView.textColor = color
Thanks for your help.