3

Is it possible to detect CGRect of link text or CGPoint of position, to show popover (on ipad) for clicked link in TTTAttributedLabel? I need to show popover on clicked link with TTTAttributedLabel.

Thanks!

Valerii Pavlov
  • 1,986
  • 1
  • 19
  • 30
  • 1
    I don't think there's any way to do this with the TTTAttributedLabel. If you just need to know where the link is, you could register where the user touches. However, that won't give you any specific point of the link like the center or beginning. – simonbs Dec 24 '12 at 15:22
  • Yeap, i transmit CGPoint of touch to my controller, and after i show popover. But i hope to search link mid point. Another question what to do with line separated links.. Anyway, any help is welcome. – Valerii Pavlov Dec 25 '12 at 13:41

2 Answers2

2

You can only do this by modifying the TTTAttributedLabel to support this behavior.

1) Modify the delegate method:

- (void)attributedLabel:(TTTAttributedLabel *)label
   didSelectLinkWithURL:(NSURL *)url
                atPoint:(CGPoint)point;

2) In the TTTAttributedLabel.m source, modify the touchesEnded:withEvent: and grab the touch point and pass this onto the delegate method.

    switch (result.resultType) {
        case NSTextCheckingTypeLink:
            if ([self.delegate respondsToSelector:@selector(attributedLabel:didSelectLinkWithURL:atPoint:)]) {
                UITouch *touch = [touches anyObject];
                CGPoint touchPoint = [touch locationInView:self];
                [self.delegate attributedLabel:self didSelectLinkWithURL:result.URL atPoint:touchPoint];
                return;

3) In the view controller, you will need to convert that touch point to be in relative coordinates to the view controller's view:

#pragma mark TTTAttributedLabelDelegate
- (void)attributedLabel:(TTTAttributedLabel *)label
   didSelectLinkWithURL:(NSURL *)url 
                atPoint:(CGPoint)point
{
        CGPoint normalizedPoint = [self convertPoint:point fromView:label];        

        UIActionSheet *actionSheet = [[UIActionSheet alloc] ...
        [actionSheet showFromRect:CGRectMake(normalizedPoint.x, normalizedPoint.y-kSensibleOffset, 10, 10)
                           inView:label
                         animated:YES];
    }
#pragma mark -
bentford
  • 33,038
  • 7
  • 61
  • 57
-2

You don't want to do this at the gesture level by trying to keep track of position. Since you have a link, just override openURL and do whatever you want when you intercept the link click. More details are here.

Community
  • 1
  • 1
vortek
  • 474
  • 2
  • 14