单击UITextView时如何获取选定的URL?

时间:2015-06-16 03:40:40

标签: ios uitextview

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

1 个答案:

答案 0 :(得分:0)

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 
}