Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3210634
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:35:44+00:00 2026-05-17T14:35:44+00:00

I have some in a touch handler which responds to a tap on a

  • 0

I have some in a touch handler which responds to a tap on a view that I’ve drawn some attributed text in. through this, I’ve got to the point where I have a CTRunRef (and the associated line) as well as the number of glyphs in that run.

What I’m not able to figure out easily, is how I can take that run of glyphs and, given my attributed string, map it out to characters in the string.

Specifically the problem is I would like to know what word the user tapped on in the view, so I can process whether or not that word is a URL and fire off a custom delegate method so I can open a web view with it. I have all the possible substrings, I just don’t know how to map where the user tapped to a particular substring.

Any help would be greatly appreciated.

UPDATE: I’ve actually gone and done it a different way, on the suggestion of another person off of stackoverflow. Basically what I’ve done is to set a custom attribute, @"MyAppLinkAddress" with the value of the URL I found when I was converting the string to an attributed string. This happens before I draw the string. Therefore, when a tap event occurs, I just check if that attribute exists, and if so, call my delegate method, if not, just ignore it. It is working how I’d like now, but I’m going to leave this question open for a few more days, if someone can come up with an answer, I’ll happily accept it if its a working solution so that some others may be able to find this information useful at some point in the future.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T14:35:44+00:00Added an answer on May 17, 2026 at 2:35 pm

    So as I mentioned in the update, I elected to go a different route. Instead I got the idea to use a custom attribute in the attributed string to specify my link, since I had it at creation time anyway. So I did that. Then in my touch handler, when a run is tapped, I check if that run has that attribute, and if so, call my delegate with it. From there I’m happily loading a webview with that URL.

    EDIT: Below are snippets of code explaining what I did in this answer. Enjoy.

    // When creating the attribute on your text store. Assumes you have the URL already. 
    // Filled in for convenience
    NSRange urlRange = [tmpString rangeOfString:@"http://www.foo.com/"];
    [self.textStore addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[UIColor blueColor].CGColor range:urlRange];
    [self.textStore addAttribute:@"CustomLinkAddress" value:urlString range:urlRange];
    

    then…

    // Touch handling code — Uses gesture recognizers, not old school touch handling.
    // This is just a dump of code actually in use, read through it, ask questions if you
    // don't understand it. I'll do my best to put it in context.
    - (void)receivedTap:(UITapGestureRecognizer*)tapRecognizer
    {
            CGPoint point = [tapRecognizer locationInView:self];
    
            if(CGRectContainsPoint(textRect, point))
            {
                    CGContextRef context = UIGraphicsGetCurrentContext();
    
                    point.y = CGRectGetHeight(self.contentView.bounds) - kCellNameLabelHeight - point.y;
    
                    CFArrayRef lines = CTFrameGetLines(ctframe);
                    CFIndex lineCount = CFArrayGetCount(lines);
                    CGPoint origins[lineCount];
                    CTFrameGetLineOrigins(ctframe, CFRangeMake(0, 0), origins);
                    for(CFIndex idx = 0; idx < lineCount; idx++)
                    {
                            CTLineRef line = CFArrayGetValueAtIndex(lines, idx);
                            CGRect lineBounds = CTLineGetImageBounds(line, context);
                            lineBounds.origin.y += origins[idx].y;
    
                            if(CGRectContainsPoint(lineBounds, point))
                            {
                                    CFArrayRef runs = CTLineGetGlyphRuns(line);
                                    for(CFIndex j = 0; j < CFArrayGetCount(runs); j++)
                                    {
                                            CTRunRef run = CFArrayGetValueAtIndex(runs, j);
                                            NSDictionary* attributes = (NSDictionary*)CTRunGetAttributes(run);
                                            NSString* urlString = [attributes objectForKey:@"CustomLinkAddress"];
                                            if(urlString && ![urlString isEqualToString:@""])
                                            {
                                                    [self.delegate didReceiveURL:[NSURL URLWithString:urlString]];
                                                    UIGraphicsPopContext();
                                                    return;
                                            }
                                    }
                            }
                    }
                    UIGraphicsPopContext();
            }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that appends some rows to a table. One of the
i have a input tag which is non editable, but some times i need
I have a login.jsp page which contains a login form. Once logged in the
I want to have generalised email templates. Currently I have multiple email templates with
I have several USB mass storage flash drives connected to a Ubuntu Linux computer
I am attempting to pull some information from my tnsnames file using regex. I
I'm in the process of porting some code from Linux to Mac OS X.
I am playing with TFS 2010, and am trying to setup a build process
I would like to get a sum from a column, with and without a
I am trying to redirect to a specific path based on HTTP_HOST or SERVER_NAME

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.