单击 UITextView 时如何选择 URL?

How to get selected URL when clicked on UITextView?

我的问题是,当我点击其中包含的任何 URL 时,我想从 UITextView 获取 URL 字符串。

UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
textView.dataDetectorTypes = UIDataDetectorTypeAll;
textView.delegate = self; 

// New in iOS 7
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange { 
    if ([[URL scheme] isEqualToString:@"username"]) { 
        NSString *username = [URL host];  
        // do something with this username 
        // ... 
        return NO; 
    } 
    return YES; // let the system open this URL 
}