diff --git a/RTLabelProject/Classes/RTLabel.h b/RTLabelProject/Classes/RTLabel.h index 3a5443b..31d6cc1 100755 --- a/RTLabelProject/Classes/RTLabel.h +++ b/RTLabelProject/Classes/RTLabel.h @@ -95,6 +95,7 @@ typedef enum @property (nonatomic, assign) int currentSelectedButtonComponentIndex; @property (nonatomic, assign) CFRange visibleRange; @property (nonatomic, assign) BOOL highlighted; +@property (nonatomic, readonly) NSMutableDictionary *anchorsBoundingRects; // set text - (void)setText:(NSString*)text; @@ -102,6 +103,9 @@ typedef enum // get the visible text - (NSString*)visibleText; +// get the bounding rect of the anchor referenced by its 'id' attribute (...), relatively to the view +- (CGRect) boundingRectForAnchorWithIdentifier: (NSString *) identifier; + // alternative // from susieyy http://github.com/susieyy // The purpose of this code is to cache pre-create the textComponents, is to improve the performance when drawing the text. diff --git a/RTLabelProject/Classes/RTLabel.m b/RTLabelProject/Classes/RTLabel.m index 4cf8624..a6afcab 100755 --- a/RTLabelProject/Classes/RTLabel.m +++ b/RTLabelProject/Classes/RTLabel.m @@ -176,6 +176,12 @@ - (void)setLineBreakMode:(RTTextLineBreakMode)lineBreakMode [self setNeedsDisplay]; } +- (CGRect) boundingRectForAnchorWithIdentifier:(NSString *)identifier +{ + NSValue *value = [_anchorsBoundingRects objectForKey:identifier]; + return value? value.CGRectValue : CGRectZero; +} + - (void)drawRect:(CGRect)rect { [self render]; @@ -280,6 +286,12 @@ - (void)render value = [value stringByReplacingOccurrencesOfString:@"'" withString:@""]; [component.attributes setObject:value forKey:@"href"]; + value = [component.attributes objectForKey:@"id"]; + if (value) { + value = [value stringByReplacingOccurrencesOfString:@"'" withString:@""]; + [component.attributes setObject:value forKey:@"id"]; + } + [links addObject:component]; } else if ([component.tagLabel caseInsensitiveCompare:@"u"] == NSOrderedSame || [component.tagLabel caseInsensitiveCompare:@"uu"] == NSOrderedSame) @@ -335,7 +347,8 @@ - (void)render if (self.currentSelectedButtonComponentIndex==-1) { // only check for linkable items the first time, not when it's being redrawn on button pressed - + _anchorsBoundingRects = @{}.mutableCopy; + for (RTLabelComponent *linkableComponents in links) { float height = 0.0; @@ -360,8 +373,12 @@ - (void)render CGFloat button_width = primaryOffset2 - primaryOffset; - RTLabelButton *button = [[RTLabelButton alloc] initWithFrame:CGRectMake(primaryOffset+origin.x, height, button_width, ascent+descent)]; + CGRect buttonFrame = CGRectMake(primaryOffset+origin.x, height, button_width, ascent+descent); + RTLabelButton *button = [[RTLabelButton alloc] initWithFrame:buttonFrame]; + if ([linkableComponents.attributes objectForKey:@"id"]) + _anchorsBoundingRects[[linkableComponents.attributes objectForKey:@"id"]] = [NSValue valueWithCGRect:buttonFrame]; + [button setBackgroundColor:[UIColor colorWithWhite:0 alpha:0]]; [button setComponentIndex:linkableComponents.componentIndex];