Krelborn / KILabel

A simple to use drop in replacement for UILabel for iOS 7 and above that highlights links such as URLs, twitter style usernames and hashtags and makes them touchable.
MIT License
470 stars 132 forks source link

Crash if string contains NSLinkAttributeName #25

Open AlexIzh opened 9 years ago

AlexIzh commented 9 years ago

If string contains NSURL in NSLinkAttributeName then this code is incorrect //KILabel.m:481 NSString *realURL = [text attribute:NSLinkAttributeName atIndex:matchRange.location effectiveRange:nil]; if (realURL == nil) realURL = [plainText substringWithRange:matchRange];//crash

Krelborn commented 9 years ago

I can't reproduce this crash on the development branch. What is the text in the label and attribute?

AlexIzh commented 9 years ago

try this attributed string: NSMutableAttributedString *string = [NSMutableAttributedString new]; [string appendAttributedString:[[NSAttributedString alloc] initWithString:@"I have "]]; [string appendAttributedString:[[NSAttributedString alloc] initWithString:@"http://google.com/" attributes:@{NSLinkAttributeName:@"http://google.com/"}]]; [string appendAttributedString:[[NSAttributedString alloc] initWithString:@", "]]; [string appendAttributedString:[[NSAttributedString alloc] initWithString:@"http://google.com/" attributes:@{NSLinkAttributeName:[NSURL URLWithString:@"http://google.com/"]}]]; [string appendAttributedString:[[NSAttributedString alloc] initWithString:@", and http://google.com"]]; label.attributedText = string;

Krelborn commented 9 years ago

Thanks for the quick reply. The problem is because KILabel is assuming NSLinkAttributeName is a string. This is actually fixed on the develop branch where we are checking type of the object (It can be either string or url but url is preferred).

I was planning on releasing the develop branch over the next few days so it's fairly stable if you want to get the fix now. Otherwise I'll close this when I've finalised the 1.1 release.

Thanks

pod 'KILabel', :branch: 'develop'