我知道我可以textView.dataDetectorTypes = UIDataDetectorTypeLink;
自动检测链接。
现在这看起来很简单,但我找不到检查UITextView
's dataDetector 是否真的找到匹配项的方法。
我正在寻找类似的东西,BOOL hasLink = textView.hasLink;
或者甚至BOOL hasLink = textView.text.hasLink;
是一种方法来引用NSDataDetector
NSDataDetector
是否没有内置方法来检查这一点,还是我必须独立确认?
编辑:
所以我只是用我自己的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;
}