Search code examples
ioshyperlinkclickuitextviewtap

How can I click a link in the UITextView and get the position of the 'tap' at the same time


I have added some links to my UITextView using NSAttributedString,and I want to show user a 'note' when they click the link.The problem is I don't know how to get the position of the tap where user tap to open the link.


Solution

  • override hitTest:withEvent method

    signature

    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
    

    Above method is supplied with CGPoint which is the location of touch.Create a CGPoint class property and store the touch-location in it.

    @property (nonatomic,assign) CGPoint previousTouch;
    
    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
       self.previousTouch = point;
       return [super hitTest:point withEvent:event];
    }