diff --git a/README.md b/README.md index e118545..a3f53f9 100644 --- a/README.md +++ b/README.md @@ -258,7 +258,9 @@ NIAttributedLabel was extracted from Nimbus 1.2.0 by [Jeff Verkoeyen](http://jef Contributors ------------ -You can be the first! [Open a pull request now](https://github.com/NimbusKit/Basics/compare/). +- [fluidsonic](https://github.com/fluidsonic) + +Add yourself when contributing! [Open a pull request now](https://github.com/NimbusKit/Basics/compare/). License ======= diff --git a/src/NIAttributedLabel.m b/src/NIAttributedLabel.m index 16a0c8a..14c29c2 100644 --- a/src/NIAttributedLabel.m +++ b/src/NIAttributedLabel.m @@ -117,7 +117,7 @@ @interface NIAttributedLabel() @property (nonatomic) CTFrameRef textFrame; // CFType, manually managed lifetime, see setter. -@property (assign) BOOL detectingLinks; // Atomic. +@property (atomic, strong) id runningLinkDetection; @property (nonatomic) BOOL linksHaveBeenDetected; @property (nonatomic, copy) NSArray* detectedlinkLocations; @property (nonatomic, strong) NSMutableArray* explicitLinkLocations; @@ -272,6 +272,7 @@ - (void)setAttributedText:(NSAttributedString *)attributedText { // Clear the link caches. self.detectedlinkLocations = nil; + self.runningLinkDetection = nil; self.linksHaveBeenDetected = NO; [self removeAllExplicitLinks]; @@ -532,9 +533,9 @@ - (void)setHighlightedTextColor:(UIColor *)highlightedTextColor { } } -- (NSArray *)_matchesFromAttributedString:(NSString *)string { +- (NSArray *)_matchesFromAttributedString:(NSString *)string textCheckingTypes:(NSTextCheckingTypes)textCheckingTypes { NSError* error = nil; - NSDataDetector* linkDetector = [NSDataDetector dataDetectorWithTypes:(NSTextCheckingTypes)self.dataDetectorTypes + NSDataDetector* linkDetector = [NSDataDetector dataDetectorWithTypes:textCheckingTypes error:&error]; NSRange range = NSMakeRange(0, string.length); @@ -542,15 +543,21 @@ - (NSArray *)_matchesFromAttributedString:(NSString *)string { } - (void)_deferLinkDetection { - if (!self.detectingLinks) { - self.detectingLinks = YES; - + if (self.runningLinkDetection == nil) { NSString* string = [self.mutableAttributedString.string copy]; + NSTextCheckingTypes textCheckingTypes = (NSTextCheckingTypes)self.dataDetectorTypes; + + self.runningLinkDetection = string; + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - NSArray* matches = [self _matchesFromAttributedString:string]; - self.detectingLinks = NO; + NSArray* matches = [self _matchesFromAttributedString:string textCheckingTypes:textCheckingTypes]; dispatch_async(dispatch_get_main_queue(), ^{ + if (self.runningLinkDetection != string) { + // link detection was cancelled + return; + } + self.detectedlinkLocations = matches; self.linksHaveBeenDetected = YES; @@ -572,7 +579,7 @@ - (void)detectLinks { [self _deferLinkDetection]; } else { - self.detectedlinkLocations = [self _matchesFromAttributedString:self.mutableAttributedString.string]; + self.detectedlinkLocations = [self _matchesFromAttributedString:self.mutableAttributedString.string textCheckingTypes:(NSTextCheckingTypes)self.dataDetectorTypes]; self.linksHaveBeenDetected = YES; } }