nsattributedstring 可点击的 iOS [英] nsattributedstring tappable iOS

查看:75
本文介绍了nsattributedstring 可点击的 iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想出了如何在核心文本中使 url 可点击,但我无法弄清楚如何使其他词可点击,例如 'my name is @george' .我希望 @george 在核心文本中可以点击.有办法吗?我正在尝试这样做以使其可点击:

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"];

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

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

感谢任何帮助!

谢谢.

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

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

推荐答案

使用 TTTAttributedLabel.支持超链接.

参考 TTTAttributedLabel-string-as-text-and-links)例如如何使用.

这篇关于nsattributedstring 可点击的 iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆