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

- 3,244
- 3
- 48
- 90
-
Check out this [thread](http://stackoverflow.com/questions/866200/disable-magnifying-glass-in-uitextfield) – Alexander May 17 '12 at 17:53
-
Answer is [HERE](https://stackoverflow.com/a/48077605/4061501). **No subclass needed.** – Lal Krishna Jan 03 '18 at 12:41
5 Answers
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.

- 56,823
- 9
- 150
- 195

- 3,244
- 3
- 48
- 90
-
2But 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
-
1Yes, 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
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;
}

- 59
- 1
- 2
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)
}

- 3,199
- 36
- 32
Xamarin.iOS:
- Create custom UITextView
- Override
GestureRecognizerShouldBegin
method on yourUITextView
public override bool GestureRecognizerShouldBegin(UIGestureRecognizer gestureRecognizer)
{
if (gestureRecognizer is UILongPressGestureRecognizer ||
gestureRecognizer.Name != "UITextInteractionNameLinkTap")
{
return false;
}
return true;
}

- 868
- 9
- 16
@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.

- 735
- 11
- 23