Skip to content

Instantly share code, notes, and snippets.

@kmussel
Created August 7, 2012 13:26
Show Gist options
  • Save kmussel/3285338 to your computer and use it in GitHub Desktop.
Save kmussel/3285338 to your computer and use it in GitHub Desktop.
- (NSTextCheckingResult *)linkAtPoint:(CGPoint)point
{
CGRect selfBounds = self.bounds;
static const CGFloat kVMargin = 5.0f;
if (!CGRectContainsPoint(CGRectInset(selfBounds, 0, -kVMargin), point))
{
return nil;
}
[self createTextFrame];
if (!self.textFrame)
return 0;
CFArrayRef lines = CTFrameGetLines(self.textFrame);
if (!lines) return nil;
CFIndex count = CFArrayGetCount(lines);
NSTextCheckingResult* foundLink = nil;
CGPoint origins[count];
CTFrameGetLineOrigins(self.textFrame, CFRangeMake(0,0), origins);
for (int i = 0; i < count; i++)
{
CGPoint linePoint = origins[i];
CTLineRef line = CFArrayGetValueAtIndex(lines, i);
CGRect flippedRect = [self getLineBounds:line point:linePoint];
flippedRect.origin.x += contentInset.left;
CGRect bounds = CGRectMake(CGRectGetMinX(selfBounds),
CGRectGetMaxY(selfBounds)-CGRectGetMaxY(selfBounds),
CGRectGetWidth(selfBounds),
CGRectGetHeight(selfBounds));
CGRect rect = CGRectMake(CGRectGetMinX(flippedRect),
CGRectGetMaxY(bounds)-CGRectGetMaxY(flippedRect),
CGRectGetWidth(flippedRect),
CGRectGetHeight(flippedRect));
rect = CGRectInset(rect, 0, -kVMargin);
if (CGRectContainsPoint(rect, point))
{
CGPoint relativePoint = CGPointMake(point.x-CGRectGetMinX(rect),
point.y-CGRectGetMinY(rect));
CFIndex idx = CTLineGetStringIndexForPosition(line, relativePoint);
foundLink = ([self linkAtIndex:idx]);
if (foundLink) return foundLink;
}
}
return nil;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment