1

我知道我可以textView.dataDetectorTypes = UIDataDetectorTypeLink;自动检测链接。

现在这看起来很简单,但我找不到检查UITextView's dataDetector 是否真的找到匹配项的方法。

我正在寻找类似的东西,BOOL hasLink = textView.hasLink;或者甚至BOOL hasLink = textView.text.hasLink;是一种方法来引用NSDataDetectorNSDataDetector

是否没有内置方法来检查这一点,还是我必须独立确认?

编辑:

所以我只是用我自己的NSDataDetector来做到这一点,(下面的代码)但问题仍然是是否真的没有办法提取检测到的数据类型或确认检测到的数据类型在 a 中是否存在UITextView

这是我用来检查自己的:

-(BOOL) doesTextHaveLink:(NSString *)text {
    NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
    NSArray *matches = [linkDetector matchesInString:text options:0 range:NSMakeRange(0, [text length])];
    for (NSTextCheckingResult *match in matches) {
        if ([match resultType] == NSTextCheckingTypeLink) {
            return YES;
        }
    }
    return NO;
}
4

1 回答 1

0

请使用此代码,如果 textview 有 url,则返回 yes

-(BOOL)checkUrl:(NSString *)strText{
    NSString *urlRegEx =
    @"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
    NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx]; 
    return [urlTest evaluateWithObject:strText];
}
于 2016-07-16T04:18:58.380 回答