【发布时间】:2021-06-09 10:20:20
【问题描述】:
我正在尝试在文本中设置具有多个链接的 UITextView。我的实现基于here 描述的建议。大多数链接都按预期工作,但是点击其中一些链接会使应用程序崩溃并出现以下错误EXC_BREAKPOINT (code=1, subcode=0x185646694):
Crash error
Call stack
我的 UITextView 配置代码:
private var actionableLinks = [(String, ()->Void)]() // each link = (actionableString, tapAction)
private func setupMessageText() {
guard messageTextView != nil else { return }
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.paragraphSpacingBefore = 16
let attributedText = NSMutableAttributedString(string: messageText, attributes: [
.font: messageTextView.font!,
.foregroundColor: messageTextView.textColor!,
.paragraphStyle: paragraphStyle
])
addActionableLinks(to: attributedText)
messageTextView?.attributedText = attributedText
}
private func addActionableLinks(to attributedText: NSMutableAttributedString) {
actionableLinks.forEach {
let actionableString = $0.0
if let nsRange = messageText.nsRange(of: actionableString) {
attributedText.addAttribute(.link, value: actionableString, range: nsRange)
}
}
}
为了处理点击动作,我已经实现了正确的 UITextViewDelegate 方法:
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
let tappedLinkString = URL.absoluteString
if let link = actionableLinks.first(where: { $0.0 == tappedLinkString }) {
let tapAction = link.1
tapAction()
return false
}
return true
}
我的这个屏幕的故事板配置(我在故事板中设置了 UITextView 委托):
Storyboard Configuration
任何见解将不胜感激!谢谢。
【问题讨论】:
-
我怀疑该链接无效。
if let nsRange = messageText.nsRange(of: actionableString), URL(string: actionableString) != nil {可能是解决办法。如果可以重现,能否提供导致问题的actionableString? -
请参阅 stackoverflow.com/questions/65656646/… 和 stackoverflow.com/questions/48154641/…,因为这可能是问题所在(有解释)
-
感谢您的评论和很好的见解。我用作链接的字符串实际上只是一个普通字符串而不是 URL。我的实现基于this previous answer,并且没有提到任何内容。导致崩溃的链接的字符串是“新条款和条件”,成功的是“支持”。
-
如前所述(在解释中请参阅两个链接),如果“链接”是
"new terms and conditions",则转换为URL,由Apple 在内部使用URL(string: thatString),就在调用textView(_:shouldInteractWith:in:)之前,它为零并使其崩溃。,因为空间无效。百分比转义它们(参见第二个链接问题),并可能向它们添加一个方案:yourApp://作为前缀。 -
确实不错!在阅读了您链接的问题后,我产生了怀疑。我马上测试一下
标签: ios swift xcode uitextview uitextviewdelegate