在UITextView中单击NSURL [英] Clicking on NSURL in a UITextView

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

问题描述

我的 UITextView 在我的 UIView 中跨越了100.0分。

I have a UITextView that spans 100.0 points across my UIView.

UITextView 中,我有使用以下函数捕获的链接:

In the UITextView, I have links that get captured with the following function:

- (BOOL) textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange

这非常适合捕捉某些字符,但我遇到一个问题:如果链接是文本视图中的最后一个字符,那么点击将一直按到该行。

This works great to capture certain characters but I have one problem: if the link is the last characters in the text view, then the tap will get pressed all the way across that line.

因此,如果我的文本视图中包含以下文本,其中@test是链接:

So if I have the text view with the following text where @test is the link:

// The entire remainder of the line will be the link (all the white space after @test)
Hello @test

我如何解决这个问题?

推荐答案

对我而言,它只突出了链接...我错过了什么吗?

For me it only highlights the link... Am I missing something?

更新:

这是一个通过虚拟URL的真正hacky解决方案:

Here's a really hacky solution via dummy URL's:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSMutableAttributedString* attributedString = [[NSMutableAttributedString alloc] init];
    [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Lorem ipsum dolor sit amet, vim iuvaret blandit intellegebat ut. Solet diceret interpretaris eos cu, magna dicat explicari mei ex, cibo adversarium eu pro. Ei odio saepe eloquentiam cum, nisl case nec ut. Harum habemus definiebas et vix, est cu aeque sonet, in his salutatus repudiare deterruisset. Quo duis autem intellegat an, regione propriae et vis."]];

    NSAttributedString* dummyUrl = [[NSAttributedString alloc] initWithString:@" " attributes:@{ NSLinkAttributeName : @"http://dummy.com" }];
    NSAttributedString* url = [[NSAttributedString alloc] initWithString:@"http://stackoverflow.com" attributes:@{ NSLinkAttributeName : @"http://stackoverflow.com" }];
    [attributedString appendAttributedString:dummyUrl];
    [attributedString appendAttributedString:url];
    [attributedString appendAttributedString:dummyUrl];
    self.textView.attributedText = attributedString;
}

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    return ![URL.absoluteString isEqualToString:@"http://dummy.com"];
}

基本上你强制 UITextView 将stackoverflow链接之前和之后的抽头识别为虚拟链接。由于它只是一个空间,它是不可见的,但不幸的是,如果你在stackoverflow链接之前/之后点击并按住,你会看到用灰色突出显示的空格,尽管 shouldInteractWithURL 返回 NO
不幸的是,除非你从头开始实现你自己的 UITextField ,否则你似乎无法绕过这种行为......

Basically you force the UITextView to recognise the tap before and after the stackoverflow link as the dummy link. Since it's just a space it's invisible, however unfortunately if you tap and hold before/after the stackoverflow link you'll see the space highlighted with gray, despite shouldInteractWithURL returning NO. Unfortunately it seems you cannot circumvent this behaviour unless you implement your own UITextField from scratch...

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

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