UITextView link detection in iOS 7

后端 未结 17 1302
再見小時候
再見小時候 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:35

    Be aware, that your textview will only recognize the links if not editable!

    Here is a nice tutorial on how to make an editable UITextView with `link detection``

    Editable UITextView with link detecion

    I've not experienced any problems with that solution since now.

    The trick is a GestureRecognizer forwaring touches and enabling/disabling the editing.

    You could apply the same thing with the selectable / not selectable issue on iOS7

    0 讨论(0)
  • 2020-12-01 10:36

    If you are adding UITextview programmatically just add below lines:

            _textView.userInteractionEnabled = YES;
            _textView.dataDetectorTypes = UIDataDetectorTypeLink;
            _textView.scrollEnabled = NO;
            _textView.editable = NO;
    

    This worked for me.

    0 讨论(0)
  • 2020-12-01 10:37

    None of the above worked for me, instead I did this:

    [self.textView setDataDetectorTypes:UIDataDetectorTypeNone];
    [self.textView.setTextColor:[UIColor whiteColor]];
    [self.textView setDataDetectorTypes:UIDataDetectorTypeNone];
    

    I did this with my textview that was supposed to detect all types, and which had non detected color set to white. You can change the code to represent your proper color and link types to detect.

    0 讨论(0)
  • 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! :)

    0 讨论(0)
  • 2020-12-01 10:42

    Deactivating UITextViews scrolling ability did the trick for me in a similar setup.

    0 讨论(0)
  • 2020-12-01 10:43

    I had the same issue and disabling scrolling on the UITextView activates the link detection on load rather than only working once the user has interacted with the textview. The UITextView also had to be selectable and non-editable.

    detailTextView.scrollEnabled = NO;
    detailTextView.editable = NO;
    detailTextView.selectable = YES;
    

    Being selectable or having scroll enabled isn't necessary on iOS6.

    Another thing to check is that userinteraction is enabled on the cell and content view of the cell, otherwise the link won't be clickable.

    0 讨论(0)
提交回复
热议问题