Daibaku
Daibaku

Reputation: 12566

Swift how to detect specific String in uilabel

I would like to add UITapGestureRecognizer to label and detect which specific text tapped. for instance text on the label is hi, how are you And if user tapped hi then I would like to detect that and print that text.

I tried like this but can't add UITapGestureRecognizer to the element. I understand because element doesn't have that property.

  guard let text = textLabel.text? else { return }
        let separateText = text.components(separatedBy: ",")
            separateText.forEach { (element) in
                    let myGesture = UITapGestureRecognizer(target: self, action: #selector(handleDelete))
                element.isUserInteractionEnabled = true
                element.addGestureRecognizer(myGesture)      
      }

  @objc func handleDelete() {
     print(element)
    }

How can I achieve what I want? Should I take different approach? Thank you in advance!

Upvotes: 0

Views: 656

Answers (1)

Mateusz
Mateusz

Reputation: 1224

Wow, quite interesting task :)

Your approach won't work as String is not UI element and can not be interactable like other UI elements.

One approach could be adding tap gesture recogniser to UILabel and base on tap position. But this will be hard especially when you need to support different languages (some for example have deferent read direction or can have "hi" in different place in text).

Another one could be using NSAttributedString. You could add URL (via NSAttributedStringKey link) to attributed string and handle it. Defect of this approach is fact that you have to use UITextView as UILabel doesn't support url handling.

Simplified code:

    func setup(textView: UITextView) {
        let text = NSMutableAttributedString(string: "Hi ", attributes: [.link: URL(string: "myDeeplink://textClickRecognizer?text=Hi")!])
        let otherLinkText = NSAttributedString(string: "how are you ", attributes: [.link: URL(string: "myDeeplink://textClickRecognizer?text=how%20are%20you")!])

        text.append(otherLinkText)

        textView?.attributedText = text
        textView?.isEditable = false
        textView?.dataDetectorTypes = .link
        textView?.delegate = self  
    }

    // UITextViewDelegate
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {

        //you should check if link is supported / expected one
       //....

        let components = URLComponents(url: URL, resolvingAgainstBaseURL: false)!
        let queryItem = components.queryItems?.first!

        print(queryItem?.value)

        return false
    }  

Upvotes: 1

Related Questions