ios - 如何让UILabel响应点击?

我发现我可以比UITextField更快地创建UILabel,并且我计划在大多数时间使用UILabel作为我的数据显示应用程序。

总而言之,我希望让用户点击UILabel并让我的回调响应。 那可能吗?

谢谢。

Happy asked 2019-09-10T19:33:44Z
10个解决方案
200 votes

您可以在UILabel中添加UITapGestureRecognizer实例。

例如:

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapped)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[myLabel addGestureRecognizer:tapGestureRecognizer];
myLabel.userInteractionEnabled = YES;
pythonquick answered 2019-09-10T19:34:01Z
36 votes

如果您正在使用故事板,则可以在故事板中执行此整个过程而无需其他代码。 在故事板上添加标签,然后在标签上添加一个点击手势。 在“实用工具”窗格中,确保选中“已启用用户交互”标签。 从点击手势(在故事板中视图控制器的底部),按住Ctrl键并单击并拖动到ViewController.h文件并创建一个Action。 然后在ViewController.m文件中实现该操作。

leenyburger answered 2019-09-10T19:34:25Z
12 votes

Swift 3.0

初始化手势为tempLabel

tempLabel?.text = "Label"
let tapAction = UITapGestureRecognizer(target: self, action: #selector(self.actionTapped(_:)))
tempLabel?.isUserInteractionEnabled = true
tempLabel?.addGestureRecognizer(tapAction)

动作接收器

func actionTapped(_ sender: UITapGestureRecognizer) {
    // code here
}

Swift 4.0

初始化手势为tempLabel

tempLabel?.text = "Label"
let tapAction = UITapGestureRecognizer(target: self, action:@selector(actionTapped(_:)))
tempLabel?.isUserInteractionEnabled = true
tempLabel?.addGestureRecognizer(tapAction)

动作接收器

func actionTapped(_ sender: UITapGestureRecognizer) {
    // code here
}
Koushik answered 2019-09-10T19:35:07Z
8 votes

Swift 2.0:

我正在添加一个nsmutablestring作为示例Label的文本,启用用户交互,添加点击手势并触发方法。

override func viewDidLoad() {
    super.viewDidLoad()

    let newsString: NSMutableAttributedString = NSMutableAttributedString(string: "Tap here to read the latest Football News.")
    newsString.addAttributes([NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleDouble.rawValue], range: NSMakeRange(4, 4))
    sampleLabel.attributedText = newsString.copy() as? NSAttributedString

    let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapResponse:")
    tapGesture.numberOfTapsRequired = 1
    sampleLabel.userInteractionEnabled =  true
    sampleLabel.addGestureRecognizer(tapGesture)

}
func tapResponse(recognizer: UITapGestureRecognizer) {
    print("tap")
}
A.G answered 2019-09-10T19:35:33Z
4 votes

您可以使用UIButton,并将文本设置为您想要的。 如果您不想,按钮不必看起来像按钮

Matt S. answered 2019-09-10T19:35:57Z
2 votes

在UILabel上添加Tap手势

UITapGestureRecognizer *tapAction = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lblClick:)];
tapAction.delegate =self;
tapAction.numberOfTapsRequired = 1;

//Enable the lable UserIntraction
lblAction.userInteractionEnabled = YES;
[lblAction addGestureRecognizer:tapAction];   

并评估选择器方法

- (void)lblClick:(UITapGestureRecognizer *)tapGesture {

}

注意:在.h文件中添加UIGestureRecognizerDelegate

Hardik Thakkar answered 2019-09-10T19:36:34Z
2 votes

Swift版本:viewDidLoad()

然后在viewDidLoad()内添加:

  let yourLbl=UILabel(frame: CGRectMake(x,y,width,height)) as UILabel!

    yourLbl.text = "SignUp"
    tapGesture.numberOfTapsRequired = 1
    yourLbl.addGestureRecognizer(tapGesture)
    yourLbl.userInteractionEnabled = true
    tapGesture.addTarget(self, action: "yourLblTapped:")
Aditya Jha answered 2019-09-10T19:37:05Z
1 votes

如果要在按钮中使用多行文本,请创建带有多行文本的UILabel,并将其作为子视图添加到按钮中。

例如:

yourLabel=[Uilabel alloc]init];
yourLabel.frame=yourButtom.Frame;//(frame size should be equal to your button's frame)
[yourButton addSubView:yourLabel]
Chandramani answered 2019-09-10T19:37:36Z
1 votes

来自Alvin George的Swift 3

override func viewDidLoad() {
    super.viewDidLoad()
    let newsString: NSMutableAttributedString = NSMutableAttributedString(string: "Tap here to read the latest Football News.")
    newsString.addAttributes([NSUnderlineStyleAttributeName: NSUnderlineStyle.styleDouble.rawValue], range: NSMakeRange(4, 4))
    sampleLabel.attributedText = newsString.copy() as? NSAttributedString

    let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapResponse))
    tapGesture.numberOfTapsRequired = 1
    sampleLabel.isUserInteractionEnabled =  true
    sampleLabel.addGestureRecognizer(tapGesture)
}

func tapResponse(recognizer: UITapGestureRecognizer) {
    print("tap")
}
MRustamzade answered 2019-09-10T19:38:00Z
0 votes

Swift版看起来像这样:

func addGestureRecognizerLabel(){
    //Create a instance, in this case I used UITapGestureRecognizer,
    //in the docs you can see all kinds of gestures
    let gestureRecognizer = UITapGestureRecognizer()

    //Gesture configuration
    gestureRecognizer.numberOfTapsRequired = 1
    gestureRecognizer.numberOfTouchesRequired = 1
    /*Add the target (You can use UITapGestureRecognizer's init() for this)
    This method receives two arguments, a target(in this case is my ViewController) 
    and the callback, or function that you want to invoke when the user tap it view)*/
    gestureRecognizer.addTarget(self, action: "showDatePicker")

    //Add this gesture to your view, and "turn on" user interaction
    dateLabel.addGestureRecognizer(gestureRecognizer)
    dateLabel.userInteractionEnabled = true
}

//How you can see, this function is my "callback"
func showDatePicker(){
    //Your code here
    print("Hi, was clicked")
}

//To end just invoke to addGestureRecognizerLabel() when
//your viewDidLoad() method is called

override func viewDidLoad() {
    super.viewDidLoad()
    addGestureRecognizerLabel()
}
LeoGalante answered 2019-09-10T19:38:24Z
translate from https://stackoverflow.com:/questions/7154340/how-to-get-uilabel-to-respond-to-tap