ios - ASTextNode 中没有 LinkAttribute 的 TouchUpInside

标签 ios swift asyncdisplaykit

我有一个文本 (ASTextNode)

let contentNode = ASTextNode()

contentNode.maximumNumberOfLines = 7
contentNode.truncationMode = .byTruncatingTail
contentNode.isUserInteractionEnabled = true
contentNode.delegate = self

并且在contentNode中有链接

我为 contentNode 设置 touchUpInSide

contentNode.addTarget(self, action: #selector(contentNodeTapped), forControlEvents: .touchUpInside)

@objc func contentNodeTapped() {

    if contentNode.maximumNumberOfLines == 7 {
      contentNode.maximumNumberOfLines = 0
    } else {
      contentNode.maximumNumberOfLines = 7
    }
    UIView.animate(withDuration: 0.2, delay: 0, options: [UIView.AnimationOptions.layoutSubviews], animations: {
      self.contentNode.setNeedsLayout()
      self.contentNode.layoutIfNeeded()
    }, completion: nil)

}

func textNode(_ textNode: ASTextNode!, tappedLinkAttribute attribute: String!, value: Any!, at point: CGPoint, textRange: NSRange) {
    guard let url = value as? URL else { return }
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

当我点击 contentNode 时,我想更改 MaximumNumberOfLines 以查看所有文本,但这并不顺利。我只是尝试了 UIView.animate 但没有效果。

当我点击 contentNode 中的链接时,它不会同时调用 2 个函数 contentNodeTappedtappedLinkAttribute

如何在点击链接时仅调用tappedLinkAttribute

Text after tapped

点击后的文本

Text after tapped

最佳答案

我相信你不能这样做,而是需要使用 ASEditableTextNode 并使用 ASEditableTextNodeDelegateurl 点击时捕获事件

你可以打电话

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {

  // your action here

   return true
}

您需要确保您的 ASEditableTextNode 样式看起来像您当前的 ASTextNode

关于ios - ASTextNode 中没有 LinkAttribute 的 TouchUpInside,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55862981/

相关文章:

objective-c - 为什么在 Safari 中打开 URL 时 Table Cell 不会立即取消选择?

ios - PrimaryViewController的UINavigationBar嵌入到UITabBarController后变色

ios - 当 applicationWillTerminate 时快速启动应用程序

ios - 如何使用 URLSeassion 显示下载进度

ios - SizeToFit异步显示套件

swift - AsyncDisplayKit - ASButton 大小

iphone - iOS:可执行文件已使用无效的权利进行签名

ios - 找到 UISlider 指向的方向

swift - 如何在 UIAlertController 中的 UITextField 之后插入 UILabel