如何让UILabel响应点击?

我发现我可以比UITextField更快地创buildUILabel,而且我计划大部分时间使用UILabel来显示数据。

长话短说,我希望让用户点击一个UILabel,让我的callback回应。 那可能吗?

谢谢。

您可以将UITapGestureRecognizer实例添加到您的UILabel。

例如:

 UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapped)]; tapGestureRecognizer.numberOfTapsRequired = 1; [myLabel addGestureRecognizer:tapGestureRecognizer]; myLabel.userInteractionEnabled = YES; 

如果您正在使用故事板,则可以在故事板中完成整个过程,无需额外的代码。 添加一个标签到故事板,然后添加一个点击手势标签。 在“实用程序”窗格中,确保为标签选中了“用户交互已启用”。 从轻击手势(在视图控制器的底部,在故事板中),按住Ctrl并单击并拖动到您的ViewController.h文件并创build一个Action。 然后在ViewController.m文件中实现该操作。

Swift 2.0:

我添加一个nsmutablestring作为sampleLabel的文本,启用用户交互,添加轻击手势并触发一个方法。

 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") } 

你可以使用一个UIButton,并将文本设置为你想要的。 如果你不想要的话,button不必像button一样

Swift 3.0

初始化tempLabel的手势

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

动作接收器

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

如果你想在你的button中使用多行文本,那么创build一个带有多行文字的UILabel ,并作为子视图添加到你的button中。

例如:

 yourLabel=[Uilabel alloc]init]; yourLabel.frame=yourButtom.Frame;//(frame size should be equal to your button's frame) [yourButton addSubView:yourLabel] 

在UILable上添加“点按”手势

 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]; 

并评估select器的方法

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

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

来自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") } 

Swift版本: var tapGesture : UITapGestureRecognizer = UITapGestureRecognizer()

然后在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:") 

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() }