How to make a UILabel clickable?
Asked Answered
B

12

173

I would like to make a UILabel clickable.

I have tried this, but it doesn't work:

class DetailViewController: UIViewController {

    @IBOutlet weak var tripDetails: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        ...
        let tap = UITapGestureRecognizer(target: self, action: Selector("tapFunction:"))
        tripDetails.addGestureRecognizer(tap)
    }

    func tapFunction(sender:UITapGestureRecognizer) {
        print("tap working")
    }
}
Beltz answered 11/11, 2015 at 19:39 Comment(2)
What is the frame of your UILabel? Are you sure you're touching in the label's frame? Do you have an UIViews covering the label? Is userInteractionEnabled set to True for the label?Catamite
Make sure your UILabel IBOutlet is hooked up from your Nib or StoryboardYulandayule
R
209

Have you tried to set isUserInteractionEnabled to true on the tripDetails label? This should work.

Ralston answered 11/11, 2015 at 19:48 Comment(3)
thanks for the answer!!! Here I have another issue still about UITapGestureRecognizer: #33659578Beltz
I am trying to change background color on tap but the tap event is firing when i release my finger. Is there a way to catch touch down event? Long press is not what i am looking for.Recor
oh, wow so label by default is not user interact-able, appreciate it!Sayyid
Q
132

Swift 3 Update

Replace

Selector("tapFunction:")

with

#selector(DetailViewController.tapFunction)

Example:

class DetailViewController: UIViewController {

    @IBOutlet weak var tripDetails: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        ...

        let tap = UITapGestureRecognizer(target: self, action: #selector(DetailViewController.tapFunction))
        tripDetails.isUserInteractionEnabled = true
        tripDetails.addGestureRecognizer(tap)
    }

    @objc
    func tapFunction(sender:UITapGestureRecognizer) {
        print("tap working")
    }
}
Quartern answered 12/10, 2016 at 6:51 Comment(1)
I had to annotate the tap function with @objc.Marque
T
54

SWIFT 4 Update

 @IBOutlet weak var tripDetails: UILabel!

 override func viewDidLoad() {
    super.viewDidLoad()

    let tap = UITapGestureRecognizer(target: self, action: #selector(GameViewController.tapFunction))
    tripDetails.isUserInteractionEnabled = true
    tripDetails.addGestureRecognizer(tap)
}

@objc func tapFunction(sender:UITapGestureRecognizer) {

    print("tap working")
}
Terryterrye answered 8/11, 2017 at 10:14 Comment(0)
F
31

Swift 5

Similar to @liorco, but need to replace @objc with @IBAction.

class DetailViewController: UIViewController {

    @IBOutlet weak var tripDetails: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        ...

        let tap = UITapGestureRecognizer(target: self, action: #selector(DetailViewController.tapFunction))
        tripDetails.isUserInteractionEnabled = true
        tripDetails.addGestureRecognizer(tap)
    }

    @IBAction func tapFunction(sender: UITapGestureRecognizer) {
        print("tap working")
    }
}

This is working on Xcode 10.2.

Fugere answered 16/7, 2019 at 16:10 Comment(1)
@IBAction is only needed to expose method to Xcode's Interface Builder (hence IB). It also acts as @objc but if you add the gestures or other actions with code you should use the @objc annotationDoornail
B
16

Swift 3 Update

yourLabel.isUserInteractionEnabled = true
Batten answered 6/10, 2016 at 11:28 Comment(2)
this didn't help me. I am trying to do it on a label in a tableviewTutu
made it working here as well, even if it was already set in the Storyboard.Imminence
I
16

Good and convenient solution:

In your ViewController:

@IBOutlet weak var label: LabelButton!

override func viewDidLoad() {
    super.viewDidLoad()

    self.label.onClick = {
        // TODO
    }
}

You can place this in your ViewController or in another .swift file(e.g. CustomView.swift):

@IBDesignable class LabelButton: UILabel {
    var onClick: () -> Void = {}
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        onClick()
    }
}

In Storyboard select Label and on right pane in "Identity Inspector" in field class select LabelButton.

Don't forget to enable in Label Attribute Inspector "User Interaction Enabled"

Incunabulum answered 10/6, 2018 at 9:53 Comment(1)
Inside viewDidLoad() need to add this line if doesn't work: self.label.isUserInteractionEnabled = trueRabiah
A
4

You need to enable the user interaction of that label.....

For e.g

yourLabel.userInteractionEnabled = true

Austria answered 11/11, 2015 at 19:50 Comment(1)
this didn't help me. I am trying to do it on a label in a tableviewTutu
C
3

For swift 3.0 You can also change gesture long press time duration

label.isUserInteractionEnabled = true
let longPress:UILongPressGestureRecognizer = UILongPressGestureRecognizer.init(target: self, action: #selector(userDragged(gesture:))) 
longPress.minimumPressDuration = 0.2
label.addGestureRecognizer(longPress)
Chiao answered 21/9, 2017 at 13:43 Comment(0)
G
3

Pretty easy to overlook like I did, but don't forget to use UITapGestureRecognizer rather than UIGestureRecognizer.

Galasyn answered 14/8, 2019 at 17:11 Comment(0)
N
3

Thanks researcher

Here's my solution for programmatic user interface using UIKit.

I've tried it only on Swift 5. And It worked.

Fun fact is you don't have to set isUserInteractionEnabled = true explicitly.

import UIKit

open class LabelButon: UILabel {
    var onClick: () -> Void = {}
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        isUserInteractionEnabled = true
    }
    
    public required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    public convenience init() {
        self.init(frame: .zero)
    }
    
    open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        onClick()
    }
}

Uses:

override func viewDidLoad() {
    super.viewDidLoad()
    
    let label = LabelButton()
    label.text = "Label"
    label.onClick = {
        // TODO
    }
}

Don't forget to set constraints. Otherwise it won't appear on view.

Naima answered 20/5, 2021 at 9:54 Comment(0)
P
0

On top of all of the other answers, this depends on where the label is, it might be behind some subviews. You might think you tap on the label but maybe click the top view. To solve this you can bring the label view to the front with the following line.

self.view.bringSubviewToFront(lblView)
Petaliferous answered 20/2, 2023 at 10:6 Comment(0)
S
-4

As described in the above solution you should enable the user interaction first and add the tap gesture

this code has been tested using

Swift4 - Xcode 9.2

yourlabel.isUserInteractionEnabled = true
yourlabel.addGestureRecognizer(UITapGestureRecognizer(){
                //TODO 
            })
Semantic answered 1/2, 2018 at 12:17 Comment(1)
I get Cannot invoke initializer for type 'UITapGestureRecognizer' with an argument list of type '(() -> Void)' when I try this.Phrasal

© 2022 - 2024 — McMap. All rights reserved.