0

Hi I am trying to trigger separate click events for hyper link click and normal text click in UITextView

How to Separate hyperlink text click and and normal text click in UITextView

Hope you understand my problem.

Here is what I tried.

     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
    }
}
Kaushik Makwana
  • 142
  • 1
  • 14
Ahil NS
  • 3
  • 4
  • Does this answer your question? [How to intercept click on link in UITextView?](https://stackoverflow.com/questions/2543967/how-to-intercept-click-on-link-in-uitextview) – Keshu R. Dec 09 '19 at 08:35
  • Thank you for your response.But this is not what i am looking for. For Example i want to trigger function A when clicking the hyperlink text(shouldInteractWith URL is working fine) and I want to trigger function B when clicking other text in UITextView – Ahil NS Dec 09 '19 at 08:44
  • You can add a "fake URL" (under the `NSAttributedStringKey.Link` attribute) and check what's the URL tapped in `textView(_ textView:shouldInteractWith:)` to know what to do. – Larme Dec 09 '19 at 09:05

2 Answers2

1

Define an attributed string and than add attributes to it with defining the range where you want to tap to open the link. And don't forget to add delegate method of textView, and also do YourTextView.delegate = self in view did load.

    class ViewController: UIViewController, UITextViewDelegate {
    @IBOutlet var textView: UITextView!

    override func viewDidLoad() {
        textView.delegate = self.
        let attributedString = NSMutableAttributedString(string: "want to search something on Google! Click Here")
        attributedString.addAttribute(.link, value: "www.google.com", range: NSRange(location: 36, length: 10))

        textView.attributedText = attributedString
    }

    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        UIApplication.shared.open(URL)
        return false
    }
}
Kaushik Makwana
  • 142
  • 1
  • 14
  • Thank you for your answer.But this is not what i am looking for. For Example i want to trigger function A when clicking the hyperlink text(shouldInteractWith URL is working fine) and I want to trigger function B when clicking other text in UITextView. – Ahil NS Dec 09 '19 at 08:42
0

A possible solution is to add a "fake URL" (like a "internal URL Scheme look alike) where there is no link attribute:

attributedText.enumerateAttribute(.link, in: NSRange(location: 0, length: attributedText.length), options: []) { (attribute, range, pointee) in
    //If there is no URL => Set our custom one
    if attribute == nil {
        attributedText.addAttribute(.link, value: "com.myapp.custom", range: range)
    }
}

And in this delegate method, check the value of URL.

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
    print("url: \(URL)")
    if url == ... {} else { ... }
    return false
}
Larme
  • 24,190
  • 6
  • 51
  • 81