user2924482
user2924482

Reputation: 9120

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.

Upvotes: 3

Views: 4196

Answers (4)

Karim H
Karim H

Reputation: 1631

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 {
    
}

Upvotes: 2

JingJingTao
JingJingTao

Reputation: 1790

I don't think you can add an IBOutlet to a certain word for a UILabel. You could look at ActiveLabel, this has the functionality you need.

Here is also a link to how to open mail if you need it :D, open mail in swift.

Hope this helps and good luck.

Upvotes: 0

Kahng
Kahng

Reputation: 189

You should use attributed text to specify area to be linked in one sentence.

Maybe you can find the answer below

Upvotes: 0

Ishmeet
Ishmeet

Reputation: 705

You apparently can't make an IBAction only on apart of label. Either use a separate label or UIButton for the word email and put them right next to each other so that it appears like a single element.

Upvotes: 0

Related Questions