个人随笔记录:
- (NSMutableAttributedString *)filterLinkWithContent:(NSString *)content { NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:content]; NSError *error = NULL; NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:@"https://\\w+" options:0 error:nil]; NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:(NSTextCheckingTypes)NSTextCheckingTypeLink | NSTextCheckingTypePhoneNumber | NSTextCheckingTypeDate error:&error]; NSArray *matches = [regularExpression matchesInString:content options:0 range:NSMakeRange(0,[content length])]; for (NSTextCheckingResult *match in matches) { NSLog(@"----%@",match); if (match.range.location != NSNotFound) { [attributedString addAttribute:NSLinkAttributeName value:[content substringWithRange:match.range] range:match.range]; } if (([match resultType] == NSTextCheckingTypeDate)) { NSDate *url = [match date]; [attributedString addAttribute:NSLinkAttributeName value:url range:match.range]; } } return attributedString; }原文链接:https://www.f2er.com/regex/360831.html