如何在Swift中使单词可单击的UITextView?

时间:2019-04-26 10:15:50

标签: ios swift uitextview

有没有一种方法可以创建可点击单词的UITextView

当我单击单词时,单词将突出显示。请参考下图。

http://datatables.net/tn/4

1 个答案:

答案 0 :(得分:0)

     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")
    }
}