Thursday, January 4, 2018

Tappable links in UITextView

UITextView’s Data Detectors are helpful in detecting the urls/phone numbers in the content we display in it. 

However, links in UITextView don’t work on press, only when long pressing them works.

The work around for this is to add a Tap Gesture as shown below.


  let tapRecognizer = UITapGestureRecognizer(target: self, 
                         action: #selector(self.tappedTextView))


    @objc func tappedTextView(tapGesture: 
                                    UIGestureRecognizer) {

        let textView = tapGesture.view as! UITextView
        let tapLocation = tapGesture.location(in: textView)
        let textPosition = textView.closestPosition(to: 
        let attr: NSDictionary = textView.textStyling(at: 
                      textPosition!, in: .forward)! as NSDictionary

        if let url: NSURL = attr[] as? 
            tappedOn(url: url as URL)
    func tappedOn(url: URL) {

Objective C

  UITapGestureRecognizer *tapGesture = 
           [[UITapGestureRecognizer alloc] initWithTarget:cell 

    [sampleNotes addGestureRecognizer:tapGesture];

- (void)tappedTextView:
                    (UITapGestureRecognizer*)tapGesture {

    if (tapGesture.state != UIGestureRecognizerStateEnded) {

    UITextView *textView = (UITextView *)tapGesture.view;

    CGPoint tapLocation = [tapGesture 

    UITextPosition *textPosition = [textView 

    NSDictionary *attributes = [textView 

    NSURL *url = attributes[NSLinkAttributeName];

    if (url) {
        [self tappedOnLink:url];

Here, We are getting the tap location and checking whether the tapped area is of a link.

Hope this post is useful. Feel Free to comment incase of any queries.

No comments:

Post a Comment