Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added method to determine bounding rect of links #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions RTLabelProject/Classes/RTLabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,17 @@ 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;
+ (RTLabelExtractedComponent*)extractTextStyleFromText:(NSString*)data paragraphReplacement:(NSString*)paragraphReplacement;
// get the visible text
- (NSString*)visibleText;

// get the bounding rect of the anchor referenced by its 'id' attribute (<a href='' id=''>...</a>), 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.
Expand Down
21 changes: 19 additions & 2 deletions RTLabelProject/Classes/RTLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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];

Expand Down