UILabel touch and get the text where touched

前端 未结 5 783
一整个雨季
一整个雨季 2020-12-08 18:05

I\'ve UILabel which has some text and 2 month names like \"In the month of January and July, sun shine will be peak\" I sub classed UILabel and added touch event to it. Now

5条回答
  •  青春惊慌失措
    2020-12-08 18:20

     
    - (void)tapped:(UITapGestureRecognizer *)tap
    {
        CGPoint location = [tap locationInView:_label];
    
        NSDictionary *attribute = @{NSFontAttributeName: _label.font};
        CGFloat width = 0;
        for (int i = 0; i substringWithRange:NSMakeRange(i, 1)];
            CGSize lettersize = [letterStr sizeWithAttributes:attribute];
            width += lettersize.width;
            if (width >= location.x ) {
                NSLog(@"str: %@", letterStr);
                break;
            }
        }
    }
    

提交回复
热议问题