在NSAttributedString中检测链接上的点击 [英] Detecting taps on link in NSAttributedString

查看:40
本文介绍了在NSAttributedString中检测链接上的点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在动态构建视图并使用drawInRect:在其上打印属性文本.某些文本可能包含Web链接.如何检测对Web链接的轻击,以便可以将链接加载到UIWebView中?我不使用UILabel,UITextView或UITextField来显示文本,因为文本包含文本和图像的混合.

I'm dynamically building views and printing attributed text on them using drawInRect:. Some of the text may include web links. How can I detect taps on web links so I can load the link in a UIWebView? I'm not using a UILabel, UITextView or UITextField to display the text because the text includes a mixture of text and images.

这是我的UIView子类在drawRect中绘制文本的一些代码.

This is some of the code in drawRect for my UIView subclass that draws the text.

//draw the text

    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:self.message.text];

    [attString beginEditing];

    NSArray *arrString = [attString.string componentsSeparatedByString:@" "];
    for (NSString *str in arrString)
    {
        if ([str rangeOfString:@"http://" options:NSCaseInsensitiveSearch].location != NSNotFound ||
            [str rangeOfString:@".com"    options:NSCaseInsensitiveSearch].location != NSNotFound ||
            [str rangeOfString:@".gov"    options:NSCaseInsensitiveSearch].location != NSNotFound ||
            [str rangeOfString:@".org"    options:NSCaseInsensitiveSearch].location != NSNotFound ||
            [str rangeOfString:@".edu"    options:NSCaseInsensitiveSearch].location != NSNotFound ||
            [str rangeOfString:@".net"    options:NSCaseInsensitiveSearch].location != NSNotFound ||
            [str rangeOfString:@".xxx"    options:NSCaseInsensitiveSearch].location != NSNotFound)
        {
            NSRange rng = [attString.string rangeOfString:str];

            NSString *url;

            if ([str rangeOfString:@"http://"  options:NSCaseInsensitiveSearch].location == NSNotFound &&
                [str rangeOfString:@"https://" options:NSCaseInsensitiveSearch].location == NSNotFound)
            {
                url = [NSString stringWithFormat:@"http://%@", str];
            }
            else
            {
                url = str;
            }

            [attString addAttribute:NSLinkAttributeName value:[NSURL URLWithString:url] range:rng];
        }
    }

    [attString endEditing];

    CGSize theSize;
    theSize = [attString.string sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(CHAT_TEXT_WIDTH, FLT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
    [[UIColor blackColor] set];
    CGRect textRect = CGRectMake(0, 0, theSize.width, theSize.height);
    textRect.origin.x += 10;
    textRect.origin.y += 10;

    if (self.type == MESSAGE_BOX_RECEIVE) {
        textRect.origin.x += ARROW_WIDTH;
    }

    [attString drawInRect:textRect];

感谢您的帮助...

推荐答案

您要做的是使用TextKit绘制文本(在iOS 7中引入).这样,您还可以通过TextKit来解释水龙头.

What you want to do is draw the text with TextKit (introduced in iOS 7). This allows you to interpret taps by way of TextKit as well.

这是一个可下载的示例项目,在该项目中,我显示单词列表并检测用户点击了哪个单词(我什至通过暂时突出显示该单词来做出响应):

Here's a downloadable example project where I display a list of words and detect which word the user tapped on (and I even respond by momentarily highlighting the word):

https://github.com.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch10p543drawingWithTextKit/ch23p815attributedStringDrawing3/StyledText.swift

这篇关于在NSAttributedString中检测链接上的点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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