How to customize UILabel clickable

后端 未结 8 2124
余生分开走
余生分开走 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: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 
    
    @protocol ClickableLablelDelegate 
    
    @required
    - (void)onClickLabel:(UILabel *) label;
    
    @end
    
    @interface ClickableLable : UILabel
    
    @property (nonatomic, weak) id  delegate;
    
    @end
    
    
    #import "ClickableLable.h"
    
    @implementation ClickableLable
    
    -(void)awakeFromNib
    {
        [super awakeFromNib];
    
        [self setUserInteractionEnabled:YES];
    }
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self setBackgroundColor:[UIColor lightGrayColor]];
    }
    
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self setBackgroundColor:[UIColor clearColor]];
    
        if ([_delegate respondsToSelector:@selector(onClickLabel:)]) {
            [_delegate onClickLabel:self];
        }
    }
    
    -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self setBackgroundColor:[UIColor clearColor]];
    }
    

提交回复
热议问题