【问题标题】:Detecting taps on link in NSAttributedString检测 NSAttributedString 中的链接点击
【发布时间】:2014-03-04 06:52:23
【问题描述】:

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

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

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

感谢您的帮助...

【问题讨论】:

  • 这可能会给你一些想法。 stackoverflow.com/questions/13888941/…
  • 你能发布你的代码吗?我可以提出一些想法,但这实际上取决于您的自定义视图的工作方式。
  • @WolfLink 我编辑了我的帖子以包含我如何绘制文本的代码。谢谢。

标签: ios iphone objective-c nsattributedstring


【解决方案1】:

您要做的是使用 TextKit(在 iOS 7 中引入)绘制文本。这也允许您通过 TextKit 来解释点击。

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

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

【讨论】:

  • @DougSmith 您想要它移动到的原始版本,还是想要该示例的更新 iOS 8 / Swift 版本?
  • iOS 8/Swift 版本会很棒。
  • @DougSmith 你明白了!
  • @matt,是否也有 Objective-C 版本。 ?我们仍然生活在 Objective-C 世界中。
猜你喜欢
  • 2010-11-18
  • 1970-01-01
  • 2014-03-04
  • 2011-11-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-23
  • 1970-01-01
相关资源
最近更新 更多