Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用NSRegularExpression创建自定义NSDataDetector类型_Ios_Objective C_Uiwebview_Nsregularexpression - Fatal编程技术网

Ios 使用NSRegularExpression创建自定义NSDataDetector类型

Ios 使用NSRegularExpression创建自定义NSDataDetector类型,ios,objective-c,uiwebview,nsregularexpression,Ios,Objective C,Uiwebview,Nsregularexpression,我正在尝试为UITextView中的内容创建自定义数据检测器。我希望能够做到这样: tv.dataDetectorTypes = UIDataDetectorTypeAll; 但是,我希望使用基于以下正则表达式的自定义UIDataDetector,而不是UIDataDetectorTypeAll: NSError *error = nil; NSRegularExpression *regex = [[NSRegularExpression alloc] ini

我正在尝试为UITextView中的内容创建自定义数据检测器。我希望能够做到这样:

tv.dataDetectorTypes = UIDataDetectorTypeAll;
但是,我希望使用基于以下正则表达式的自定义UIDataDetector,而不是UIDataDetectorTypeAll:

    NSError *error = nil;        
    NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:@"(ID: [0-9]+)"
                                                                      options:NSRegularExpressionCaseInsensitive
                                                                        error:&error];
我还希望检测到的ID是一个链接,它将带有URL+ID组合()的UIWebView推送到nav堆栈。应该只有一个id。有什么想法吗


谢谢

我认为您必须解决这个问题,因为NSDataDetector仅限于预设值


看看这个答案

这花了我一整天的时间,我终于找到并修改了Ray Wenderlich的解决方案。显然,这是iOS 7中一个相当模糊的新特性

- (NSAttributedString *)makeAttributedAbstract:(NSString*)str
{
    NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:str];

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(PMID: [0-9]+)" options:kNilOptions error:nil];

    NSRange range = NSMakeRange(0, str.length);

    [regex enumerateMatchesInString:str options:kNilOptions range:range usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
        NSRange subStringRange = [result rangeAtIndex:1];
        [mutableAttributedString addAttribute:NSLinkAttributeName value:@"pmid://" range:subStringRange];
    }];

    return (NSAttributedString*)mutableAttributedString;
}


- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    if ([[URL scheme] isEqualToString:@"pmid"]) {

        NSString *pmid = [NSString stringWithFormat:@"http://www.ncbi.nlm.nih.gov/pubmed/%@", [self.sql getPmidForId:self.abstractId] ];

        NSURL *url = [NSURL URLWithString:pmid];
                NSLog(@"pmid: %@", url);
        [self pushWebViewWithURL:url];

        return NO;
    }
    return YES; // let the system open this URL
}