【发布时间】:2019-12-09 08:10:18
【问题描述】:
您好,我正在尝试为UITextView 中的超链接点击和普通文本点击触发单独的点击事件
如何区分UITextView中的超链接文本点击和普通文本点击
希望你能理解我的问题。
这是我尝试过的。
override func viewDidLoad() {
super.viewDidLoad()
let atributedHtmlText = """
Hey I dont have hyper link text so trigger func A(). Click <a href="http://www.google.com">I have hyper link so trigger func b here</a> for more information.
"""
testTV.setAttributedHtmlText(atributedHtmlText)
let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapFunction))
testTV.isUserInteractionEnabled = true
testTV.addGestureRecognizer(tap)
testTV.delegate = self
}
@objc func tapFunction(sender:UITapGestureRecognizer) {
print("tap working")
}
extension ViewController: UITextViewDelegate {
func textView(_ textView: UITextView, shouldInteractWith URL: URL,
in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
print("URL Text Tapped ", URL)
return false
}
}
【问题讨论】:
-
感谢您的回复。但这不是我想要的。例如,我想在单击超链接文本时触发函数 A(应该InteractWith URL 工作正常),我想在单击 UITextView 中的其他文本时触发函数 B
-
您可以添加一个“虚假 URL”(在
NSAttributedStringKey.Link属性下)并检查在textView(_ textView:shouldInteractWith:)中点击的 URL 是什么,以了解该怎么做。
标签: ios swift uitextview nsattributedstring