gpt4 book ai didi

ios - 在 UITextView 中点击自定义 URL 链接时,委托(delegate)接收到 nil URL

转载 作者:行者123 更新时间:2023-11-29 12:08:39 25 4
gpt4 key购买 nike

我有一个 tableViewtableViewCells cell 显示一个 textView
textView 使用带有自定义 URL 链接信息的 attributedString,在 tableView:cellForRowAtIndexPath: 中设置,如 this tutorial 所示:

NSMutableAttributedString *attributedDisplayString = [[NSMutableAttributedString alloc] initWithString:displayString];
[attributedDisplayString addAttribute:NSLinkAttributeName
value:[NSString stringWithFormat:@"username://%@", userName]
range:NSMakeRange(0, userName.length)];
cell.textView.attributedText = attributedDisplayString;

当我点击链接时,textView:shouldInteractWithURL:inRange: 在委托(delegate)中被调用,因此自定义 URL 链接已被检测到并响应。

但是,提供的 URL 是 nil

我错过了什么?

最佳答案

抱歉问得太快了。我发现了问题,但这也许对其他人有帮助:

我的变量 userName 只包含一个 space,因此无法转换为 URL

去掉空格后就可以了。

要制作一个可用于 URL 的字符串,可以在 iOS8 和更早版本中使用
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding

在 iOS9 中

stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]

关于ios - 在 UITextView 中点击自定义 URL 链接时,委托(delegate)接收到 nil URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34176865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com