UITextView link detection in iOS 7

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

    You should check out NSDataDetector.

    You can use this to find and deal with different data (links, phone numbers and more). Have a look on this site:

    http://nshipster.com/nsdatadetector/

    You can also use the dataDetectorTypes property of UITextView to set what you want to detect in code. May just be a storyboard transition problem for you.

    textView.dataDetectorTypes = UIDataDetectorTypeLink;
    
    0 讨论(0)
  • 2020-12-01 10:45

    While this thread is old, I didn’t see an answer that worked for me with Swift, so here goes for Swift 2.2

    textView.dataDetectorTypes = UIDataDetectorTypes.Link
    textView.selectable = true
    
    0 讨论(0)
  • 2020-12-01 10:48

    So using a UITextView keeping it enabled, selectable, not scrollable & links detectable is not as simple as it seems. I encountered this in iOS 8. So my solution was to do something like this in viewDidLoad and then set editable property to NO when textBox editing is done(usually would be a method like doneIsTapped). The trick here is to set editable property to NO after setting text value to textview is completed. This will enable links in the UITextview.

    - (void)viewDidLoad 
    {
        [super viewDidLoad];
        self.txtViewComment.editable = YES;
        self.txtViewComment.selectable = YES;
        self.txtViewComment.dataDetectorTypes = UIDataDetectorTypeLink;
        self.txtViewComment.scrollEnabled = NO;
    }
    

    and

    - (IBAction)doneIsTapped:(id)sender 
    {
        self.txtViewComment.text = @"set text what ever you want";
        self.txtViewComment.editable = NO; 
    }
    

    this made the links enabled in textview. Also I would recommend not to use story board at this time(or until apple fixes this problem) and just use code to avoid any unnecessary confusion. Hope this help.

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

    Check These Lines must be added to use data detector property of textview in UItableView cell.

        txtvwMsgText.userInteractionEnabled = YES;
        txtvwMsgText.dataDetectorTypes = UIDataDetectorTypeLink;
        txtvwMsgText.scrollEnabled = NO;
        txtvwMsgText.editable = NO;
        txtvwMsgText.selectable = YES;
    
    0 讨论(0)
  • 2020-12-01 10:49

    This workaround works for me:

    textView.selectable = YES;
    textView.delegate = self;
    
    - (void) textViewDidChangeSelection:(UITextView *)textView;
    {
        NSRange range = NSMakeRange(NSNotFound, 0.0);
        if ( range.length && !NSEqualRanges(range, textView.selectedRange) ) {
            textView.selectedRange = range;
        }
    }
    
    0 讨论(0)
  • 2020-12-01 10:50

    It seems that in iOS 7 link detection only works if the UITextView is selectable. So making my UITextView not selectable stopped the the link detection from working.

    I also tested this in iOS 6 and I can confirm that in iOS 6 the link detection works fine even with the UITextView not being selectable.

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