swift - 如何在 Swift 5 中单击 HyperLink 时推送视图控制器
问题描述
嗨,我必须在点击 HyperLink 时推送一个 viewController。我有 NSMutableAttributedString 文本。该属性文本有两个超链接示例 GoToHomeScreen和aboutUs都是超链接点击aboutUs需要打开一个工作正常的链接
let attributesString = NSMutableAttributedString(string: "GoToHomeScreen for know more about app and for info aboutUs ")
attributesString.addAttribute(.link, value: "https://apple.com/aboutUs", range: NSRange(location: 50, length: 7))
textFields.isEditable = false
textFields.delegate = self
textFields.attributedText = attributesString
打开 aboutUs 工作正常,但是我们如何在点击 GoToHomeScreen 时推送视图控制器
注意:- 我正在使用 UITextView 执行它,我不能使用按钮
解决方案
您还需要为 GoToHomeScreen 添加一个链接。
attributesString.addAttribute(.link, value: "myApp://GoToHomeScreen", range: ...)
由于您已经设置了委托,请实现textView(_:shouldInteractWith:in:interaction:)
:
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
if URL.absoluteString == "myApp://GoToHomeScreen" {
pushHomeScreenVC()
return false
}
return true
}