I've added a link detector to TTTAttributedLabel that identifies @mentions and #hashtags and creates a link at that position in my TTTAttributedLabel instance:
- (void)highlightMentionsInString:(NSString *)text withColor:(UIColor *)color isBold:(BOOL)bold isUnderlined:(BOOL)underlined
{
NSRegularExpression *mentionExpression = [NSRegularExpression regularExpressionWithPattern:@"(?:^|\\s)(@\\w+)" options:NO error:nil];
NSArray *matches = [mentionExpression matchesInString:text
options:0
range:NSMakeRange(0, [text length])];
for (NSTextCheckingResult *match in matches) {
NSRange matchRange = [match rangeAtIndex:1];
NSString *mentionString = [text substringWithRange:matchRange];
NSRange linkRange = [text rangeOfString:mentionString];
NSString* user = [mentionString substringFromIndex:1];
NSString* linkURLString = [NSString stringWithFormat:@"user:%@", user];
[self.attributedLabel addLinkToURL:[NSURL URLWithString:linkURLString] withRange:linkRange];
}
}
I've also discovered that I can do this to easily change the link colors and attributes:
NSArray *keys = [[NSArray alloc] initWithObjects:(id)kCTForegroundColorAttributeName,(id)kCTUnderlineStyleAttributeName
, nil];
NSArray *objects = [[NSArray alloc] initWithObjects:color,[NSNumber numberWithInt:kCTUnderlineStyleNone], nil];
NSDictionary *linkAttributes = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
self.attributedLabel.linkAttributes = linkAttributes;
But this changes the colors for every link attribute - including web links, hashtags, and mentions. Is there a way to create different link colors using a regular expression or range? Say if I wanted @mentions to be gray, @hashtags to be red, and web links to be blue?