Disable Magnifying Glass in UITextview

2019-01-14 23:04发布

问题:

In UITextview when touch is pressed for the longer time magnifying glass shows up. How can i disable it.

回答1:

Finally this issue is also resolved

Here is the code for reference in case anyone needs

in the m file of subclassed UITextview added code

-(void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
{
    //Prevent zooming but not panning
    if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) 
    {
        gestureRecognizer.enabled = NO;
    }
    [super addGestureRecognizer:gestureRecognizer];
    return;
}

It works.



回答2:

This works for me

@implementation CustomTextView 

- (NSArray *)selectionRectsForRange:(UITextRange *)range
{
    self.selectedTextRange = nil;
    return nil;
}

- (void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
    gestureRecognizer.delegate = self;

    [super addGestureRecognizer:gestureRecognizer];
    return;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    return NO;
}

- (CGRect)caretRectForPosition:(UITextPosition *)position
{
    return [super caretRectForPosition:self.endOfDocument];
}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if (([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] && !gestureRecognizer.delaysTouchesEnded))
    {
        return NO;
    }
    else
        return YES;

}


回答3:

@Irina's answer works partially (Try a tap followed by a long press and you will have a magnifying overlay) for iOS 9.x and crashes on iOS 10 with the following:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'You cannot change the delegate of the UIViewControllerPreviewing failure relationship gesture recognizer'

The following code works both for iOS 9.x and 10.x in every combination of tap and/or long gestures I could think of.

Note I don't guarantee that it will be accepted by Apple's review.

@implementation CustomTextView

- (void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
    NSArray *allowedGestures = @[ @"UILongPressGestureRecognizer", @"UIScrollViewDelayedTouchesBeganGestureRecognizer", @"UIScrollViewPanGestureRecognizer" ];

    if (![allowedGestures containsObject:NSStringFromClass([gestureRecognizer class])])
    {
        return;
    }

    if (([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] && !gestureRecognizer.delaysTouchesEnded))
    {
        return;
    }

    [super addGestureRecognizer:gestureRecognizer];
}

@end

We need UIScrollViewDelayedTouchesBeganGestureRecognizer and UIScrollViewPanGestureRecognizer in order to keep the UITextView ability to scroll. Both classes are part of private API so use that at your own risk.



回答4:

Swift 4 version of @user1120133's answer:

    override func addGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer) {
      //Prevent long press to show the magnifying glass
      if gestureRecognizer is UILongPressGestureRecognizer {
        gestureRecognizer.isEnabled = false
      }

      super.addGestureRecognizer(gestureRecognizer)
    }