【发布时间】:2016-11-19 07:41:41
【问题描述】:
我知道我可以通过 textView.dataDetectorTypes = UIDataDetectorTypeLink; 自动检测链接。
现在这看起来很简单,但我找不到检查 UITextView 的 dataDetector 是否真的找到匹配项的方法。
我正在寻找 BOOL hasLink = textView.hasLink; 或 BOOL hasLink = textView.text.hasLink; 之类的东西,甚至可能是一种方法来提取对 NSDataDetector 的引用(我可能会使用 NSDataDetector 方法来检查,但我必须确认,只是抛出想法)
是否没有内置方法可以检查这一点,还是我必须独立确认?
编辑:
所以我只是使用我自己的NSDataDetector 来执行此操作,(代码如下)但问题仍然是是否真的无法提取检测到的数据类型或确认 @ 中存在检测到的数据类型987654328@?
这是我用来检查自己的:
-(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;
}
【问题讨论】:
-
你想检查 textView contane url 与否?
标签: objective-c uitextview nsurl nsdatadetector