From 941a6f81c20f6d59c05c0ef6fcc8192662bf7f6e Mon Sep 17 00:00:00 2001 From: mattdonnelly Date: Sun, 10 Nov 2013 23:39:18 +0000 Subject: [PATCH] Fixed URL detection error, fixes #2 --- MDHTMLLabel/MDHTMLLabel.m | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/MDHTMLLabel/MDHTMLLabel.m b/MDHTMLLabel/MDHTMLLabel.m index 5fe3396..4f45c02 100644 --- a/MDHTMLLabel/MDHTMLLabel.m +++ b/MDHTMLLabel/MDHTMLLabel.m @@ -350,7 +350,10 @@ - (NSString *)detectURLsInText:(NSString *)text { if (match.resultType == NSTextCheckingTypeLink) { - if (match.range.location > 6 && ![[text substringWithRange:NSMakeRange(match.range.location-6, 4)] isEqualToString:@"href"]) + // if the URL location is greater than 6 (enough space for an anchor tag) and there's a 'href' before it, or if there's a closing anchor tag + // after it, then we dont wrap the URL in anchor tags + if (!(match.range.location > 6 && [[text substringWithRange:NSMakeRange(match.range.location-6, 4)] isEqualToString:@"href"]) + && (![[text substringWithRange:match.range] rangeOfString:@"a>"].length > 0)) { text = [text stringByReplacingCharactersInRange:match.range withString:[NSString stringWithFormat:@"%@", match.URL.absoluteString, match.URL.absoluteString]];