NSDataDetector * _detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSArray *_urlMatches = [_detector matchesInString:self.model.content options:0 range:NSMakeRange(0, self.model.content.length)];
for (NSTextCheckingResult *match in _urlMatches) {
if ([match resultType] == NSTextCheckingTypeLink) {
NSRange matchRange = [match range];
if ([self isIndex:index inRange:matchRange]) {
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:matchRange];
}
else {
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:matchRange];
}
[attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:matchRange];
}
}
网友评论