在ASTextNode中没有LinkAttribute的TouchUpInside

时间:2019-04-26 07:42:32

标签: 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

1 个答案:

答案 0 :(得分:0)

我相信您无法做到这一点,相反,您需要使用ASEditableTextNode并使用ASEditableTextNodeDelegate来捕获url时捕获的事件

您可以打电话

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

  // your action here

   return true
}

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