// 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
}