UITextView implements a method that only allows link interaction and does not allow picture selection

  • 2021-12-04 11:28:32
  • OfStack

Detect Link

As we all know, UITextVview can automatically detect links in text in the following ways:


let label = UITextView()
label.dataDetectorTypes = .link
label.isEditable = false

If we use attributedString, we will find that if we add Attachment, a selection will be triggered (edited GR). If we only want linked gr and don't want selected gr, we can traverse and disable it. The method is as follows:

Portal: http://stackoverflow.com/questions/18962742/uitextview-link-detection-in-ios-7

Objective-C


NSArray *textViewGestureRecognizers = self.captionTextView.gestureRecognizers;
NSMutableArray *mutableArrayOfGestureRecognizers = [[NSMutableArray alloc] init];
for (UIGestureRecognizer *gestureRecognizer in textViewGestureRecognizers) {
 if (![gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {
  [mutableArrayOfGestureRecognizers addObject:gestureRecognizer];
 } else {
  UILongPressGestureRecognizer *longPressGestureRecognizer = (UILongPressGestureRecognizer *)gestureRecognizer;
  if (longPressGestureRecognizer.minimumPressDuration < 0.3) {
   [mutableArrayOfGestureRecognizers addObject:gestureRecognizer];
  }
 }
}
self.captionTextView.gestureRecognizers = mutableArrayOfGestureRecognizers;

Summarize

The above is the whole content of this article. I hope the content of this article can bring 1 certain help to everyone's study or work. If you have any questions, you can leave a message for communication.


Related articles: