ios – 为UILabel的一部分添加点击手势
2020年02月19日
作者:佚名
我有一个像这样的NSAttributedString:
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"testing it out @clickhere"]; NSInteger length = str.length; [str addAttribute:NSForegroundColorAttributeName value:[UIColor bestTextColor] range:NSMakeRange(0,length)];
NSMutableAttributedString设置为UILabel,如下所示:
label.attributedText = str;
如何在另一个视图控制器中为上面的字符串中的’@clickhere’做出一个轻击手势(或可点击的东西)?
谢谢!
解决方法
我认为,最好的方法是将UIGestureRecognizer添加到您的UILabel并验证您想要的帧.
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; [_yourLabel addGestureRecognizer:singleTap]; - (void)handleTap:(UITapGestureRecognizer *)tapRecognizer { CGPoint touchPoint = [tapRecognizer locationInView: _yourLabel]; //Modify the validFrame that you would like to enable the touch //or get the frame from _yourLabel using the NSMutableAttributedString,if possible CGRect validFrame = CGRectMake(0,300,44); if(YES == CGRectContainsPoint(validFrame,touchPoint) { //Handle here. } }
以上是来客网为你收集整理的ios – 为UILabel的一部分添加点击手势全部内容,希望文章能够帮你解决ios – 为UILabel的一部分添加点击手势所遇到的程序开发问题。
如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。