Search code examples
ioscore-textnsattributedstring

Coretext make part of a nsmutableattributtedstring linkable


Suppose i have a CoreText NSMutableAttributedString with this words:"Continue Reading"

I want to make the first word "Continue" clickable so that i can tap on it and show a load another UIView. I have found how to make it underlined and change the colour to blue but how can i make it linkable. Any example?

This is my code to make it underlined and the foreground colour blue.

CFAttributedStringSetAttribute(string, CFRangeMake(0, 10),kCTForegroundColorAttributeName, [UIColor blueColor].CGColor);
          SInt32 type = kCTUnderlineStyleSingle;

CFNumberRef underline = CFNumberCreate(NULL, kCFNumberSInt32Type, &type);
CFAttributedStringSetAttribute(string, CFRangeMake(0, text.length),kCTUnderlineStyleAttributeName, underline);
CFAttributedStringSetAttribute(string, CFRangeMake(0, text.length),kCTUnderlineColorAttributeName, [UIColor blueColor].CGColor);

Thanks


Solution

  • There are plenty of resources out there, one which is found here Hyperlink in coretext

    What you need to do is find the bounds of the word/section that you want to make clickable, and add a subview within those bounds that handles a tap. Hope it helps!