Tap Gesture on part of UILabel

前端 未结 5 1011
[愿得一人]
[愿得一人] 2020-12-10 18:56

I could successfully add tap gestures to a part of UITextView with the following code:

UITextPosition *pos = textView.endOfDocument;// textView ~ UITextView
         


        
5条回答
  •  独厮守ぢ
    2020-12-10 19:09

    Try this. Let your label be label :

      //add gesture recognizer to label
      UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] init];
      [label addGestureRecognizer:singleTap];
      //setting a text initially to the label
      [label setText:@"hello world i love iphone"];
    
     - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
     {
    
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self.view];
    
    CGRect rect = label.frame;
    CGRect newRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width/2, rect.size.height);
    
    if (CGRectContainsPoint(newRect, touchPoint)) {
        NSLog(@"Hello world");
    }
    }
    

    Clicking on the first half of label will work (It gives log output). Not the other half.

提交回复
热议问题