【问题标题】:How to make a word clickable UITextView in Swift?如何在 Swift 中制作一个单词可点击的 UITextView?
【发布时间】:2019-04-26 10:15:49
【问题描述】:

有没有办法创建一个可以点击单词的UITextView

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

【问题讨论】:

标签: ios swift uitextview


【解决方案1】:
     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")
    }
}

【讨论】:

  • 谢谢伙计。但是我有一个问题,所以如果我想让每个单词都可以点击,那么我必须让每个单词都成为一个范围?
  • 让 typeCasteToStringFirst = textField.text 作为 NSString? let newString = typeCasteToStringFirst?.replacingCharacters(in: range, with: string) 你可以试试这个。
  • 当然没有问题。
猜你喜欢
  • 1970-01-01
  • 2014-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-18
  • 1970-01-01
相关资源
最近更新 更多