允许来自UITextView中句子的单个URL

时间:2014-01-28 11:26:33

标签: ios ios7

我想要完成的工作如下。我有一个UItextview,其中一个句子在感知中有一个网址。我需要能够允许在URL中添加UItextview唯一URL,同时从任何句子中显示的单个字符中获取总NSString这是NSString之内,例如:

假设我在UItextView上有NSString *samplestr = @"This is a sample of a http://google.com/efg.php?EFAei687e3EsA sentence with a URL within it.";

{{1}}

1 个答案:

答案 0 :(得分:3)

我认为您已查看以下代码

NSString *string = @"your string with url ";
NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSArray *matches = [linkDetector matchesInString:string options:0 range:NSMakeRange(0, [string length])];
for (NSTextCheckingResult *match in matches) {
  if ([match resultType] == NSTextCheckingTypeLink) {
    NSURL *url = [match URL];
    NSLog(@"found URL: %@", url);
  }
}