// While Loading the view
[self underlineLabel:label withFont:[UIFont systemFontOfSize:17]];
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myAction:)];
[label addGestureRecognizer:gr];
gr.numberOfTapsRequired = 1;
//
- (CGFloat)widthOfString:(NSString *)string withFont:(UIFont *)font {
CGSize stringSize = [string sizeWithFont:font];
CGFloat width = stringSize.width;
return width;
}
- (CGFloat)heightOfString:(NSString *)string withFont:(UIFont *)font {
CGSize stringSize = [string sizeWithFont:font];
CGFloat height = stringSize.height;
return height;
}
-(void)underlineLabel:(UILabel *)lbl withFont:(UIFont *)fnt {
float wdth = [self widthOfString:lbl.text withFont:fnt];
float higt = [self heightOfString:lbl.text withFont:fnt];
UIView *viw = [[UIView alloc]initWithFrame:CGRectMake((lbl.frame.size.width - wdth)/2, (lbl.frame.size.height/2) + (higt/2), wdth, 2)];
[viw setBackgroundColor:[UIColor bluecolor]];
[lbl addSubview:viw];
}
// To Add action for gesture recognizer:
- (void)myAction:(UITapGestureRecognizer *)gr {
// Define actions here
}
爲什麼你不使用服裝類型的UIButton ..? –
第一件事是用戶永遠不會將光標移動到UILabel上。第二 - 如果你不想標籤的下劃線,只需將輕拍手勢附加到uilabel並添加目標。 – NightFury
@ user2750542你應該使用自定義類型的UIButton。並設置其標題標籤(您現在將其作爲標籤文本提供)。 – hpp