【发布时间】:2019-04-26 10:15:49
【问题描述】:
【问题讨论】:
-
是的,有可能,但你怎么知道哪个词是可点击的?
-
使用 UITapGestureRecognizer 我可以找到被点击的单词的位置,然后计算文本范围,最后得到具有该文本范围的单词。我通过搜索“ios uitextview get tapped word”找到了一些解决方案
标签: ios swift uitextview
【问题讨论】:
标签: ios swift uitextview
let textTest = "I have read and agree with the Privacy Policy & Terms and Conditions"
let tap = UITapGestureRecognizer(target: self, action: #selector(self.tapLabel))
textTest.isUserInteractionEnabled = true
textTest.addGestureRecognizer(tap)
@IBAction func tapLabel(gesture: UITapGestureRecognizer) {
let text = (textTest)!
let termsRange = (text as NSString).range(of: "Terms and Conditions")
let privacyRange = (text as NSString).range(of: "Privacy Policy")
if gesture.didTapAttributedTextInLabel(label: attributeLabel, inRange: termsRange) {
print("Tapped terms")
let vc = self.storyboard?.instantiateViewController(withIdentifier: "TermsConditionViewController") as! TermsConditionViewController
self.navigationController?.pushViewController(vc, animated: true)
} else if gesture.didTapAttributedTextInLabel(label: attributeLabel, inRange: privacyRange) {
print("Tapped privacy")
let vc = self.storyboard?.instantiateViewController(withIdentifier: "PrivacyPolicyViewController") as! PrivacyPolicyViewController
self.navigationController?.pushViewController(vc, animated: true)
} else {
print("Tapped none")
}
}
【讨论】: