laptobbe / TSMarkdownParser

MIT License
199 stars 76 forks source link

How to support plain urls (without markdown syntax)? #31

Closed kevinrenskers closed 8 years ago

kevinrenskers commented 8 years ago

When the text has [links like this](http://example.com) everything works fine.

But in the case the text is just a link: http://example.com then the link isn't detected, like it would be on GitHub for example. Is this easy to add?

kevinrenskers commented 8 years ago

I am now combining this project with TTTAttributedLabel to handle the inline kind of links, and also for the actual click handling. Feels a bit overpowered but it does the job.

I'd much rather have TSMarkdownParser handle all link detection though, then I only need a lighter weight UILabel that handles clicks on links (NSLinkAttributeName). Any suggestions for such a thing? :)

Coeur commented 8 years ago

TTTAttributedLabel is roughly doing it like this (for a UILabel):

NSDataDetector *dataDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSMutableAttributedString *mutableAttributedString = [self.attributedText mutableCopy];
NSArray *results = [dataDetector matchesInString:self.attributedText.string options:0 range:NSMakeRange(0, self.attributedText.length)];
for (NSTextCheckingResult *result in results) {
    NSString *linkURLString = [self.attributedText.string substringWithRange:result.range];
    [mutableAttributedString addAttribute:NSLinkAttributeName
                                    value:[NSURL URLWithString:linkURLString]
                                    range:result.range];
}
self.attributedText = mutableAttributedString;
Coeur commented 8 years ago

Let's include the feature for 2.0 I'm working on it today.

Coeur commented 8 years ago

I've added the feature to branch 2.0.

Coeur commented 8 years ago

@kevinrenskers, please note that TSMarkdownParser 2.0.1 is now ofifically available on CocoaPods