Disable Magnifying Glass in UITextview

2019-01-14 22:37发布

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

4条回答
Rolldiameter
2楼-- · 2019-01-14 23:17

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.

查看更多
淡お忘
3楼-- · 2019-01-14 23:26

@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楼-- · 2019-01-14 23:27

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)
    }
查看更多
狗以群分
5楼-- · 2019-01-14 23:31

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;

}
查看更多
登录 后发表回答