nsattributedstring tappable iOS

时间:2012-12-15 03:10:04

标签: objective-c core-text nsattributedstring

我想出了如何在核心文本中使用url tappable但我无法弄清楚如何使其他单词可以像'我的名字是@george'一样。我希望@george能够在核心文本中进行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);

然后,当我点击这个单词时,我会像这样检索它:

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

但我一直得到(null)。当它是URL时不会发生这种情况!

任何帮助表示赞赏!

感谢。

我通过手势识别器检测到触摸:

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;
                }
}
}

1 个答案:

答案 0 :(得分:2)

使用TTTAttributedLabel。它支持超链接。

请参阅TTTAttributedLabel-string-as-text-and-links)以了解如何使用。