Search code examples
iosswiftuilabel

iOS: Adding an IBAction to part of UILabel


I have and UILabel and I want to add and IBAction in the word "email" to open the email client in the iPhone. Here is my code:

func setInfoLabel() {
    self.myLabel.text = "send me and email if you need more information"
}

Any of you knows how can I add the action to the word in the UILabel?

I'll really appreciate your help.


Solution

  • You should use UIButton instead of UILabel because it was created for displaying text only, however, if you still want to use it

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapAction))
        self.myLabel.addGestureRecognizer(gestureRecognizer)
    } 
    
    /* will be called when tapping on the label */
    @objc func tapAction() -> Void {
        
    }