У меня есть UITextView и NSMutableAttributedString, которые имеют интерактивную ссылку. Мне нужно это, чтобы показать другую раскадровку, но я не совсем уверен, как я могу это сделать.
То, что у меня есть, работает для внешних ссылок, но не для раскадровки. Любая помощь будет оценена.
Не уверен, что приведенный ниже код - лучший способ приблизиться к нему или нет?
class HomeViewController: UIViewController {
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
let attributedString = NSMutableAttributedString(string: "Already have an account? Log in")
attributedString.addAttribute(.link, value: "", range: NSRange(location: 25, length: 6))
textView.attributedText = attributedString
}
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
UIApplication.shared.open(URL)
return false
}
}
РЕДАКТИРОВАТЬ
После ответа NikR я обновил свой код до следующего, но все еще безуспешно. Это все еще загружает Google.
class HomeViewController: UIViewController, UITextViewDelegate {
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
let attributedString = NSMutableAttributedString(string: "Already have an account? Log in")
attributedString.addAttribute(.link, value: "https://google.com", range: NSRange(location: 25, length: 6))
textView.attributedText = attributedString
textView.isSelectable = true
textView.isEditable = false
textView.delegate = self
}
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
let loginViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "loginViewController")
show(loginViewController, sender: self)
return true
}
}