UITextView link detection in iOS 7

后端 未结 17 1305
再見小時候
再見小時候 2020-12-01 10:22

I have a UITextView which is managed via Interface Builder. As data detection I have \"Links\" checked. In iOS 6 everything is working fine and links are highli

17条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 10:42

    After few tests, I found solution.

    If you want links active and you won't selection enabled, you need to edit gestureRecognizers.

    For example - there are 3 LongPressGestureRecognizers. One for click on link (minimumPressDuration = 0.12), second for zoom in editable mode (minimumPressDuration = 0.5), third for selection (minimumPressDuration = 0.8). This solution removes LongPressGestureRecognizer for selecting and second for zooming in editing mode.

    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;
    

    Tested on iOS 9, but it should work on all versions (iOS 7, 8, 9). I hope it helps! :)

提交回复
热议问题