我试图在带有可点击链接的 UITextview 中显示属性字符串。我已经创建了一个简单的测试项目来查看我哪里出错了,但仍然无法弄清楚。我已尝试启用用户交互并设置 shouldInteractWithURLs 委托(delegate)方法,但它仍然无法正常工作。这是我的代码(对于只包含 TextView 的 View Controller )
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let string = "Google"
let linkString = NSMutableAttributedString(string: string)
linkString.addAttribute(NSLinkAttributeName, value: NSURL(string: "https://www.google.com")!, range: NSMakeRange(0, string.characters.count))
linkString.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue", size: 25.0)!, range: NSMakeRange(0, string.characters.count))
textView.attributedText = linkString
textView.delegate = self
textView.selectable = true
textView.userInteractionEnabled = true
}
下面是我实现的委托(delegate)方法:
func textViewShouldBeginEditing(textView: UITextView) -> Bool {
return false
}
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
return true
}
这仍然无效。我已经搜索过这个主题,但没有任何帮助。非常感谢。
最佳答案
关于ios - 如何在 UITextView 中显示可点击的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34425096/