//Write a regular expression to match a simple string below expression is to find the urls within a string
NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:@"(? i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))" options:NSRegularExpressionCaseInsensitive error:NULL];
// description is your text
and descriptionLabel is your TTTAttributedLabel
Assign the delegate
descriptionLabel.delegate = self;
Find the match against the regular expression
[regexp enumerateMatchesInString:description options:0 range:NSMakeRange(0, [description length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags __unused, BOOL *stop __unused){
NSURL *url = [NSURL URLWithString:[description substringWithRange:match.range]];
[descriptionLabel addLinkToURL:url withRange:match.range];
}];
Here is the OnClick Method:
- (void)attributedLabel:(TTTAttributedLabel *)label
didSelectLinkWithURL:(NSURL *)url
{
NSlog("%@",label.text);
}