我目前正在使用 NSDataDetectors 查找链接:
-(void)setBodyText
{
NSString* labelText = [[message valueForKey:@"body"]gtm_stringByUnescapingFromHTML];
NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSArray *matches = [linkDetector matchesInString:labelText options:0 range:NSMakeRange(0, [labelText length])];
for (NSTextCheckingResult *match in matches) {
if ([match resultType] == NSTextCheckingTypeLink) {
NSURL *url = [match URL];
[bodyLabel addCustomLink:url inRange:[match range]];
NSLog(@"found URL: %@", url);
}
}
[bodyLabel setText:labelText];
[Utils alignLabelWithTop:bodyLabel];
}
如何使用 NSDataDetectors 解析 @ 例如:
"Hello @sheehan, you are cool"
我希望它能够检测到@sheehan
注意:我想使用 NSDataDetector 或正则表达式模式匹配。没有客户标签或控件等。