iOS给label设置click事件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33351713/article/details/76295494

以新用户按钮为例直接上代码:
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是设置的主题颜色,具体声明方式见我的其他博客。

猜你喜欢

转载自blog.csdn.net/qq_33351713/article/details/76295494