iOS给label设置click事件

以新用户按钮为例直接上代码:
UILabel *registerLabel=[[UILabel alloc] initWithFrame:CGRectMake(iconWidth+70, iconWidth+300, 80, 30)];
NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:@”新用户” attributes:attribtDic];
registerLabel.attributedText=attribtStr;
registerLabel.userInteractionEnabled=YES;
UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(registerUser:)];
[registerLabel addGestureRecognizer:labelTapGestureRecognizer];
registerLabel.layer.cornerRadius=6;
registerLabel.textColor=delegate.zhuTiColor;
[self.view addSubview:registerLabel];
其中iconWidth是屏幕宽度的一半,delegate.zhuTiColor是设置的主题颜色,具体声明方式见我的其他博客。

你可能感兴趣的:(iOS)