cdu*_*dub 5 objective-c nsurl uitextview ios ios9
我有一个UITextView横跨我的100.0分UIView.
在UITextView,我有链接,使用以下函数捕获:
- (BOOL) textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
Run Code Online (Sandbox Code Playgroud)
这非常适合捕获某些字符,但我有一个问题:如果链接是文本视图中的最后一个字符,那么点击将一直按到该行.
因此,如果我的文本视图中包含以下文本,其中@test是链接:
// The entire remainder of the line will be the link (all the white space after @test)
Hello @test
Run Code Online (Sandbox Code Playgroud)
我该如何解决?
对我来说,它只突出显示链接......我错过了什么吗?
更新:
这是一个通过虚拟 URL 实现的非常 hacky 的解决方案:
- (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"];
}
Run Code Online (Sandbox Code Playgroud)
基本上,您强制将UITextViewstackoverflow 链接之前和之后的点击识别为虚拟链接。因为它只是一个空间,所以它是不可见的,但不幸的是,如果您在 stackoverflow 链接之前/之后点击并按住,您将看到该空间以灰色突出显示,尽管shouldInteractWithURL返回了NO。不幸的是,除非您UITextField从头开始实现自己的行为,否则您似乎无法规避这种行为......
| 归档时间: |
|
| 查看次数: |
1321 次 |
| 最近记录: |