【问题标题】:Clicking on NSURL in a UITextView在 UITextView 中单击 NSURL
【发布时间】:2017-10-25 16:48:40
【问题描述】:

我有一个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

我该如何解决这个问题?

【问题讨论】:

  • 意思是要空格打开链接?
  • 如果@test 链接带有空格,它会突出显示其余部分吗?它只发生在 iOS 9 上吗?
  • 我不希望 @test 之后的任何东西都可以链接,但默认情况下它是
  • 尝试在NSAttributedString 末尾添加[[NSAttributedString alloc] initWithString:@"\u2063" attributes:nil]
  • 很酷,确实有效。我认为它有助于属性:nil。我可以理解为什么他们会越界,因为现在更难点击了

标签: ios objective-c uitextview ios9 nsurl


【解决方案1】:

对我来说,它只突出显示链接...我错过了什么吗?

更新:

这是一个通过虚拟 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"];
}

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

【讨论】:

  • 是的,但是如果您点击/单击突出显示的 .com 右侧的 20 个点会怎样?
  • 我明白你的意思。但不仅仅是链接之后的区域,它也是链接左侧、上方和下方的区域。基本上,链接的点击区域超出了可见边界(如果附近没有其他链接),以使点击链接更容易。如果您可以自定义此行为,我会感到惊讶。
  • 所以基本上不要管它,不要碰(即他们不会让你)
  • 刚刚注意到您在 prezi 工作?我们用来展示和找工作的酷软件
  • @chris 要么你必须接受这种行为,要么尝试我已经更新我的答案的黑客。老实说,我会推荐前者。另外,继续使用prezi! :)
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    在将 textView 链接到 textview 委托、可编辑和 dataDetectorTypes 等代码后,您只需设置一些属性

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

    Lorem ipsum dolor sit er elit lamet,consectetaur cillium adipisicing pecu,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua。 Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat。 https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson3.html Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur。 http://stackoverflow.comExceptioneur sint 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

    为客观-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
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      • 2014-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多