// 给字符串中的链接添加颜色及下划线 关键点在与 NSTextCheckingTypeLink
-(NSAttributedString *) attributedString:(NSString *)text {
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:text];
NSError *error;
NSDataDetector *dataDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error];
NSArray *arrayOfAllMatches = [dataDetector matchesInString:text options:NSMatchingReportProgress range:NSMakeRange(0, text.length)];
for (NSTextCheckingResult *match in arrayOfAllMatches) {
// 下划线颜色
[string addAttribute:NSForegroundColorAttributeName value:[UIColor cyanColor] range:match.range];
[string addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:match.range];
}
}
网友评论