Madhu
Madhu

Reputation: 994

Disable Magnifing glass in UITextview, but not URL Link and Phone number events

I have an issue with UITextview in iOS9, when i do a longpress on textview it shows the magnifying glass. I tried to disable through UILongPressGestureRecognizer, it completely disables the Link and phone touch events also.

How to Disable only Magnifing glass.

 override func addGestureRecognizer(gestureRecognizer: UIGestureRecognizer) {
    if gestureRecognizer .isKindOfClass(UILongPressGestureRecognizer){
    gestureRecognizer.enabled = false
    }
    super.addGestureRecognizer(gestureRecognizer)
}

the textview will be in a collectionview cell.

Upvotes: 1

Views: 991

Answers (3)

Yuan
Yuan

Reputation: 35

According to Vlada's anwser, for my code I found the delegate assigned to the magnifier activation is "UITextLoupeInteraction"

Here is my code:

open override func gestureRecognizerShouldBegin(_ gestureRecognizer:  UIGestureRecognizer) -> Bool {
    if let gestureDelegate = gestureRecognizer.delegate {
        print(gestureDelegate.description)
        if(gestureDelegate.description.localizedCaseInsensitiveContains("_UIKeyboardBasedTextSelectionInteraction")){
            return false
        }

        if(gestureDelegate.description.localizedCaseInsensitiveContains("UITextLoupeInteraction")){
            return false
        }

    }
    return true
}

My development environment: swift4, Xcode10.1, iOS 12.1

Upvotes: 1

Vlada
Vlada

Reputation: 141

There is a way to achieve that. Just override the gestureRecognizerShouldBegin for the UITextView. This gesture-recognizer object is about to begin processing touches to determine if its gesture is occurring. The only tricky part is to properly recognize delegate which is assigned to the magnifier activation.


I've successfully tested with: UITextGestureClusterLoupe. Here is the working example (Swift 4):

override public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool
{
    if let gestureDelegate = gestureRecognizer.delegate {

        if(gestureDelegate.description.localizedCaseInsensitiveContains("UITextGestureClusterLoupe"))
        {
            return false;
        }
    }
    return true;
}

Upvotes: 4

Percolator
Percolator

Reputation: 523

The code seems right but you need to override the gestureRecognizer for the textView, not the superclass. Change super.addGestureRecognizer(gestureRecognizer) to yourTextView.addGestureRecognizer(gestureRecognizer) and place it in the view controller containing an outlet to your text view(if it's not there already).

Upvotes: 0

Related Questions