点击UITextView中的NSURL

我有UITextView横跨我的UIView 100.0点。

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

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

这很好的捕捉特定的字符,但我有一个问题:如果链接是文本视图中的最后一个字符,那么点击将被按下。

所以,如果我有以下文本的文本视图@test是链接:

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

我该如何解决?

对我来说,它只突出了链接…我错过了什么?

在这里输入图像说明

更新:

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

 - (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链接之前和之后识别tap作为虚拟链接。 由于它只是一个空间,所以不幸的是,如果你在stackoverflow链接之前/之后点击并按住,你会看到用灰色突出显示的空间,尽pipeshouldInteractWithURL返回NO 。 不幸的是,似乎你不能规避这种行为,除非你从头开始实施你自己的UITextField

正如TamásZahola在前面的回答中所build议的,您可以通过在真实URL前后添加“虚拟”URL来解决此问题。 但是,而不是在虚拟链接中使用空格,使用零宽度空间unicode字符:

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

在这里输入图像说明 在将textView链接到像textview委托,editable和dataDetectorTypes这样的代码之后,您只需设置一些属性即可

下面我添加了我用过的内容

Lorem ipsum dolor sit er elit lamet,consectetaur cillium adipisicing pecu,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua。 如果你想让自己的工作变得更加简单, https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson3.html Duis aute irure dolor in reninderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur。 http://stackoverflow.com/exploader.html occaecat cupidatat non proident,sunt in culpa qui officia deserunt mollit anim id est laborum。 Nam liber te conscient to factor tum poen legum odioque civiuda http://stackoverflow.com中文

对于Objective-C

 textView.delegate=self: textView.editable = NO; textView.dataDetectorTypes = UIDataDetectorTypeLink; - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange { return true; 

}

为斯威夫特

 textView1.delegate=self textView1.editable = false textView1.dataDetectorTypes = UIDataDetectorTypes.Link func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool { return true }