Search code examples
objective-ccore-textnsattributedstring

nsattributedstring tappable iOS


I figured out how to make in core text a url tappable but i can not figure out how to make other words tappable like 'my name is @george' . I would like @george to be tappable in core text. Is there a way? I am trying like this to make it tappable:

CFAttributedStringSetAttribute(string, CFRangeMake( 0, mystring.length ), kCTFontAttributeName, ctFontBold );
        CFAttributedStringSetAttribute(string, CFRangeMake( mystring.length, linestring.length ), kCTFontAttributeName, ctFont);
        CFAttributedStringSetAttribute(string, CFRangeMake( mystring.length, linestring.length ), kCTParagraphStyleAttributeName, paragraphStyle);
         CFAttributedStringSetAttribute(string, CFRangeMake(0, mystring.length), kCTForegroundColorAttributeName, [UIColor blackColor].CGColor);
             CFAttributedStringSetAttribute(string, CFRangeMake(0, mystring.length),
                                            (CFStringRef)@"CustomLink",mystring);

Then when i tap on the word i retrieve it like this:

NSString* myString = [attributes objectForKey:@"CustomLink"];

but i get (null) all the time. This does not happen when it is a URL!

Any help appreciated!

Thanks.

I detect the touch by a gesture recogniser:

CTFrameRef ctFrame = CTFramesetterCreateFrame( framesetter, CFRangeMake(0, text.length),path, NULL );


    CFArrayRef lines = CTFrameGetLines(ctFrame);

    CGPoint* lineOrigins = malloc(sizeof(CGPoint)*CFArrayGetCount(lines));


    NSInteger index=0;

    int ii=0;

    for(CFIndex i = 0; i < CFArrayGetCount(lines); i++)
    {

        CGFloat y;

        CTFrameGetLineOrigins( ctFrame, CFRangeMake(0, 0), lineOrigins);


        CTLineRef line = (CTLineRef)CFArrayGetValueAtIndex(lines, i);
        CGPoint origin = lineOrigins[i];
        y = bottomLabel.bounds.origin.y +  bottomLabel.bounds.size.height - origin.y;
        ii=i;
        if (reversePoint.y > origin.y) {

            index = CTLineGetStringIndexForPosition(line, reversePoint);
            CFArrayRef runs = CTLineGetGlyphRuns(line);



            for(CFIndex j = 0; j < CFArrayGetCount(runs); j++)
            {
                CTRunRef run = CFArrayGetValueAtIndex(runs, j);

                CGRect runBounds;

                CGFloat ascent;//height above the baseline
                CGFloat descent;//height below the baseline
                runBounds.size.width = CTRunGetTypographicBounds(run, CFRangeMake(0, 0), &ascent, &descent, NULL);
                runBounds.size.height = ascent + descent;

                CGFloat xOffset = CTLineGetOffsetForStringIndex(line, CTRunGetStringRange(run).location, NULL);
                runBounds.origin.x = origin.x + rect.origin.x + xOffset;
                runBounds.origin.y = y;//+ rect.origin.y;
                runBounds.origin.y -= (descent+ascent)-5;


                NSDictionary* attributes = (NSDictionary*)CTRunGetAttributes(run);
                NSString* urlString = [attributes objectForKey:@"CustomLink"];
                               if(urlString && ![urlString isEqualToString:@""])
                {



                    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(showwebview:) userInfo:urlString repeats:NO];



                    return;
                }
}
}

Solution

  • Use TTTAttributedLabel. It supports hyperlink.

    Refer TTTAttributedLabel-string-as-text-and-links) for example how to use.