3

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.

dan
  • 9,695
  • 1
  • 42
  • 40
user2924482
  • 8,380
  • 23
  • 89
  • 173
  • Possible duplicate of [Create tap-able "links" in the NSAttributedText of a UILabel?](http://stackoverflow.com/questions/1256887/create-tap-able-links-in-the-nsattributedtext-of-a-uilabel) – dan Jul 08 '16 at 22:08
  • Unfortunately, you can't do this. Either add a button, or a UITextView with a link. – Pranav Wadhwa Jul 08 '16 at 22:52

4 Answers4

2

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 {
    
}
Mia
  • 1,226
  • 1
  • 19
  • 29
Karim H
  • 1,543
  • 10
  • 24
  • But this it would add the action to all the UILabel. There is no way to just be add it to the word in the UILabel? – user2924482 Jul 08 '16 at 22:06
  • Unless you render font glyphs manually and track sizes and position which is very complicated – Karim H Jul 15 '16 at 04:00
0

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.

Ishmeet
  • 707
  • 6
  • 18
0

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

Maybe you can find the answer below

Community
  • 1
  • 1
Kahng
  • 189
  • 1
  • 8
0

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.

Community
  • 1
  • 1
JingJingTao
  • 1,760
  • 17
  • 28