检测NSString中的URL

时间:2012-06-13 08:54:09

标签: ios regex cocoa-touch uilabel uitextview

我目前在UITextView中使用UITableViewCell以使链接可点击,但这会导致性能非常差。

所以我想知道是否可以检测到NSString中的链接,如果有链接,请使用UITextView,否则只需使用UILabel

3 个答案:

答案 0 :(得分:14)

绝对。使用NSDataDetector(NSDataDetector Class Reference

答案 1 :(得分:2)

我猜您熟悉检测网址的正则表达式,因此为了在您的单元格中获取一种或另一种类型的视图,您只需从UITableViewCell方法返回两个不同的tableView:cellForRowAtIndexPath: s

它可能看起来像这样(请注意,在未经测试的浏览器中输入):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *dataString = // Get your string from the data model

    // Simple pattern found here: http://regexlib.com/Search.aspx?k=URL
    NSString *URLpattern = @"^http\\://[a-zA-Z0-9\-\.]+\\.[a-zA-Z]{2,3}(/\\S*)?$";

    NSError *error = NULL;
    NSRegularExpression *URLregex = [NSRegularExpression regularExpressionWithPattern:URLpattern
                                                                              options:NSRegularExpressionCaseInsensitive
                                                                                error: &error];

    NSUInteger numberOfMatches = [URLregex numberOfMatchesInString:string
                                                    options:0
                                                      range:NSMakeRange(0, [string length])];

    if ( numberOfMatches == 0 ) {
        static NSString *PlainCellIdentifier = @"PlainCellIdentifier";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
        }
        cell.textLabel.text = timeZoneWrapper.localeName;
    }
    else {
        static NSString *FancyCellIdentifier = @"FancyCellIdentifier";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
        }

        // Configure cell view with text view here
    }

    return cell;
}

答案 2 :(得分:1)

使用这段代码,您可以使用NSDataDetector在UILable 中找到并获取 http url:

NSDataDetector* detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
                NSArray* matches = [detector matchesInString:yourString options:0 range:NSMakeRange(0,  [yourString. length])];
                NSLog(@"%@",matches) ;
                NSMutableAttributedString *MylabelAttributes = 
    [[NSMutableAttributedString alloc] initWithString:yourString];
                for (int index = 0 ; index < matches.count; index ++) {
                NSTextCheckingResult *textResult = [matches objectAtIndex : index]; 
                NSTextCheckingType textResultType = textResult.resultType;
                NSRange testRange = textResult.range;
                NSURL *testUrl = textResult.URL ;}

After applying this code, you will be able to attribute your `UILabel` text:

    [MylabelAttributes addAttribute:NSLinkAttributeName value:testUrl  range: testRange];
    [MylabelAttributes addAttribute:NSFontAttributeName                       
    value:[UIFont boldSystemFontOfSize:7.0]range:NSMakeRange(0,yourString.length)];