12

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

user1120133
  • 3,244
  • 3
  • 48
  • 90

5 Answers5

34

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.

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
user1120133
  • 3,244
  • 3
  • 48
  • 90
  • 2
    But if I am not mistaken this disables long press totally. In other words, if your text view contains a URL link or a phone number (in case you enabled detections) then on long press your text view won't be able to respond to them, am I right? – antf May 17 '12 at 19:16
  • Looking to also disable the caret and text editing? See this answer->[link](http://stackoverflow.com/questions/18139234/prevent-editing-of-text-in-uitextfield-and-hide-cursor-caret-magnifying-glass-wh/18139235#18139235) – Josh Bernfeld Aug 09 '13 at 02:40
  • 1
    Yes, like @antf said, this code totally disable touches on links, phones and events. – sig May 14 '15 at 10:41
  • having same issue in ios9, i need to diasble only maginging glass, but not url and phone touch events. – Madhu Oct 07 '15 at 09:32
  • @Madhu check the answer I just posted. It might give you some insights. – Otávio Oct 25 '16 at 14:49
1

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;

}
1

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)
    }
Bill Chan
  • 3,199
  • 36
  • 32
1

Xamarin.iOS:

  1. Create custom UITextView
  2. Override GestureRecognizerShouldBegin method on your UITextView
public override bool GestureRecognizerShouldBegin(UIGestureRecognizer gestureRecognizer)
{
    if (gestureRecognizer is UILongPressGestureRecognizer ||
        gestureRecognizer.Name != "UITextInteractionNameLinkTap")
    {
        return false;
    }
    return true;
}
Yauheni Pakala
  • 868
  • 9
  • 16
-1

@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.

Otávio
  • 735
  • 11
  • 23