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是設定的主題顏色,具體宣告方式見我的其他部落格。