How to customize UILabel clickable

后端 未结 8 2056
余生分开走
余生分开走 2020-11-30 02:28

What I want:

In an iPhone app, I\'d like to show information in a tableView. In each cell, the text is like: John recently listen to music abcdefg.mp3. and if neede

相关标签:
8条回答
  • 2020-11-30 02:51

    I think you can use textView and diable the scrolling. I did same for my project where I need to point to website addresses. just uncheck everything as shown in screenshot in 'scroll view' section in inspector window. disable scrolling

    0 讨论(0)
  • 2020-11-30 02:52

    The most simple way is to just add a gesture recognizer to the actual view (be it a UILabel or some custom view of your own). In order for the gesture recognizer to work, the view must be set userInteractionEnabled.

    Here's an example, assuming that your label view (or whatever it is) is called labelView:

    UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userTappedOnLink:)];
    // if labelView is not set userInteractionEnabled, you must do so
    [labelView setUserInteractionEnabled:YES];
    [labelView addGestureRecognizer:gesture];
    

    In this example, an action message will be sent to self and the message would be defined as

    - (void)userTappedOnLink:(UIGestureRecognizer*)gestureRecognizer;

    This works the same as wiring up any other UIControl subclass, such as a button.

    Other notes: don't try to add the same gesture recognizer to multiple views, it won't work. Don't add more than one copy of the gesture recognizer to multiple views (it doesn't replace them, it just stacks them up and wastes memory). You should add the gesture recognizer when you initially create and configure your view.

    For more information, see the documentation for UIGestureRecognizer.

    0 讨论(0)
  • 2020-11-30 02:52

    Swift 2.0 version:

      func userTappedOnLink() {
        print("clicked!")
      }
    
    ///tap and link to FB page
    let gesture = UITapGestureRecognizer(target: self, action: "userTappedOnLink")
    // if labelView is not set userInteractionEnabled, you must do so
    lblStaff.userInteractionEnabled = true
    lblStaff.addGestureRecognizer(gesture)
    
    0 讨论(0)
  • 2020-11-30 02:58

    This solution for clickable UILabel. It isn't for select link in text. Just nice solution in my opinion for clickable UILabel like UIButton:

    #import <UIKit/UIKit.h>
    
    @protocol ClickableLablelDelegate <NSObject>
    
    @required
    - (void)onClickLabel:(UILabel *) label;
    
    @end
    
    @interface ClickableLable : UILabel
    
    @property (nonatomic, weak) id <ClickableLablelDelegate> delegate;
    
    @end
    
    
    #import "ClickableLable.h"
    
    @implementation ClickableLable
    
    -(void)awakeFromNib
    {
        [super awakeFromNib];
    
        [self setUserInteractionEnabled:YES];
    }
    
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        [self setBackgroundColor:[UIColor lightGrayColor]];
    }
    
    -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        [self setBackgroundColor:[UIColor clearColor]];
    
        if ([_delegate respondsToSelector:@selector(onClickLabel:)]) {
            [_delegate onClickLabel:self];
        }
    }
    
    -(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        [self setBackgroundColor:[UIColor clearColor]];
    }
    
    0 讨论(0)
  • 2020-11-30 03:07

    You could also just put an "invisible Button" above, by using a custom button without text and images.

    enter image description here

    0 讨论(0)
  • 2020-11-30 03:08

    Swift 4.2 Version:

    var labelView = UILabel()
    let gesture = UITapGestureRecognizer(target: self, action: #selector(userTappedOnLink))
    // if labelView is not set userInteractionEnabled, you must do so
    labelView.isUserInteractionEnabled = true
    labelView.addGestureRecognizer(gesture)
    
    
    @objc func userTappedOnLink() {
        print("clicked!")
    }
    
    0 讨论(0)
提交回复
热议问题