2

我已经建立了一个ASCellNode,它工作得很好。但是,当我使用传统的时,UICollectionViewCell我使用了TTTAttributedLabel带有链接的。

我不知道我应该如何复制这个AsyncDisplayKit

我可以将 attriubtedText 从分配TTTAttributedLabel给 anASTextNode但当然它不会保留链接。我怎么能有效地做到这一点。波纹管我的例子ASCellNode

protocol EmailSentDelegator : class {
    func callSegueFromCell(data object: JSON)
}

class EmailCellNode: ASCellNode, TTTAttributedLabelDelegate {

    let cardHeaderNode: ASTextNode
    var frameSetOrNil: FrameSet?

    init(mailData: JSON) {
        // Create Nodes
        cardHeaderNode = ASTextNode()

        super.init()

        // Set Up Nodes

        cardHeaderNode.attributedString = createAttributedLabel(mailData, self).attributedText

        // Build Hierarchy
        addSubnode(cardHeaderNode)
    }

    override func calculateSizeThatFits(constrainedSize: CGSize) -> CGSize {
        var cardSize = CGSizeZero
        cardSize.width = UIScreen.mainScreen().bounds.size.width - 16

        // Measure subnodes
        let cardheaderSize = cardHeaderNode.measure(CGSizeMake(cardSize.width - 56, constrainedSize.height))
        cardSize.height = max(cardheaderSize.height,40) + subjectLabelSize.height + timeStampSize.height + emailAbstractSize.height  + 30

        // Calculate frames
        frameSetOrNil = FrameSet(node: self, calculatedSize: cardSize)
        return cardSize
    }

    override func layout() {
        if let frames = frameSetOrNil {
            cardHeaderNode.frame = frames.cardHeaderFrame
        }
    }

    func attributedLabel(label: TTTAttributedLabel!, didSelectLinkWithTransitInformation components: [NSObject : AnyObject]!) {
            self.delegate.callSegueFromCell(data: mailData)
    }

    func createAttributedLabel(mailData: JSON, cell: EmailCellNode) -> TTTAttributedLabel{
        let senderName = mailData["From"]["Name"].string!
        var recipients:[String] = []

        for (key: String, subJson: JSON) in mailData["To"] {
            if let recipientName = subJson["Name"].string {
                recipients.append(recipientName)
            }
        }
        var cardHeader = TTTAttributedLabel()
        cardHeader.setText("")
        cardHeader.delegate = cell
        cardHeader.userInteractionEnabled = true

        // Add sender to attributed string and save range

        var attString = NSMutableAttributedString(string: "\(senderName) to")
        let senderDictionary:[String:String] = ["sender": senderName]
        let rangeSender : NSRange = (attString.string as NSString).rangeOfString(senderName)

        // Check if recipients is nil and add undisclosed recipients
        if recipients.count == 0 {
            attString.appendAttributedString(NSAttributedString(string: " undisclosed recipients"))
            let rangeUndisclosed : NSRange = (attString.string as NSString).rangeOfString("undisclosed recipients")
            attString.addAttribute(NSFontAttributeName, value: UIFont(name: "SourceSansPro-Semibold", size: 14)!, range: rangeUndisclosed)
            attString.addAttribute(NSForegroundColorAttributeName, value: UIColor.grayColor(), range: rangeUndisclosed)
        } else {

            // Add recipients (first 5) to attributed string and save ranges for each

            var index = 0
            for recipient in recipients {
                if (index == 0) {
                    attString.appendAttributedString(NSAttributedString(string: " \(recipient)"))
                } else if (index == 5){
                    attString.appendAttributedString(NSAttributedString(string: ", and \(recipients.count - index) other"))
                    break
                } else {
                    attString.appendAttributedString(NSAttributedString(string: ", \(recipient)"))
                }
                index = index + 1
            }
        }
        cardHeader.attributedText = attString

        // Adding recipients and sender links with recipient object to TTTAttributedLabel
        cardHeader.addLinkToTransitInformation(senderDictionary, withRange: rangeSender)

        if recipients.count != 0 {
            var index = 0
            var position = senderName.length + 2
            for recipient in recipients {
                let recipientDictionary:[String: AnyObject] = ["recipient": recipient,"index": index ]
                let rangeRecipient : NSRange = (attString.string as NSString).rangeOfString(recipient, options: nil, range: NSMakeRange(position, attString.length-position))
                cardHeader.addLinkToTransitInformation(recipientDictionary, withRange: rangeRecipient)
                index = index + 1
                if (index == 5) {
                    let recipientsDictionary:[String: AnyObject] = ["recipients": recipients]
                    let rangeRecipients : NSRange = (attString.string as NSString).rangeOfString("and \(recipients.count - index) other")
                    cardHeader.addLinkToTransitInformation(recipientsDictionary, withRange: rangeRecipients)
                }
                position = position + rangeRecipient.length
            }
        }
        return cardHeader
    }
}

extension EmailCellNode {
    class FrameSet {
        let cardHeaderFrame: CGRect
        init(node: EmailCellNode, calculatedSize: CGSize) {
            var calculatedcardHeaderFrame = CGRect(origin: CGPointMake(senderPhotoFrame.maxX + 8, senderPhotoFrame.minY) , size: node.cardHeaderNode.calculatedSize)
            cardHeaderFrame = calculatedcardHeaderFrame.integerRect.integerRect
        }
    }
}
4

3 回答 3

0

我对 AsyncDisplayKit 不熟悉,但您在使用时存在一些问题TTTAttributedLabel

  • 您正在使用 初始化标签TTTAttributedLabel(),它调用init. 您必须改为使用指定的初始化程序initWithFrame:initWithCoder:,因为init不会正确初始化links数组和其他各种内部属性。在最新版本中TTTAttributedLabelinit被标记为不可用。

  • 您正在分配给该attributedText属性。请参阅以下说明TTTAttributedLabel.h

    @bugattributedText不建议直接设置,因为在尝试访问之前设置的任何链接时可能会导致崩溃。相反,调用setText:,传递一个NSAttributedString.

    您永远不应该分配给该attributedText属性。

于 2015-06-19T18:42:13.750 回答
0

我最终只使用ASTextNode它没有那么多的功能,TTTAttributedLabel但足以满足我的需要。此外,由于它很重ASCollectionView,最好完全异步。下面举一个例子,说明我是如何ASCellNode在创建 complexe 时做到这一点的ASTextNode

这是通过 segue 传递 JSON 数据的可点击名称的最终结果。

在此处输入图像描述

这是构建的简化版本NSAttributedString

func createAttributedLabel(mailData: JSON) -> NSAttributedString{ var 收件人:[String] = []

for (key: String, subJson: JSON) in mailData["To"] {
    if let recipientName = subJson["Name"].string {
        recipients.append(recipientName)
    }
}
// Add recipients to attributed string and save ranges for each
    var index = 0
    var position = senderName.length + 2
    for recipient in recipients {
            let recipientDictionary:[String: AnyObject] = ["recipient": recipient,"index": index ]
            let recipientJSON = mailData["To"][index]
            attString.appendAttributedString(NSAttributedString(string: ", \(recipient)"))
            let rangeRecipient : NSRange = (attString.string as NSString).rangeOfString(recipient, options: nil, range: NSMakeRange(position, attString.length-position))
            attString.addAttributes([
                kLinkAttributeName: recipientJSON.rawValue,
                NSForegroundColorAttributeName: UIColor.blackColor(),
                NSFontAttributeName:  UIFont(name: "SourceSansPro-Semibold", size: 14)!,
                NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleNone.rawValue],
                range: rangeRecipient)
    }
    index = index + 1
return attString

}

然后结束以检测链接抽头。我必须将我的 JSON 转换为原始值才能传递数据。

func textNode(textNode: ASTextNode!, tappedLinkAttribute attribute: String!, value: AnyObject!, atPoint point: CGPoint, textRange: NSRange) {

//    The node tapped a link; open it if it's a valid URL
    if  (value.isKindOfClass(NSDictionary)) {
        var jsonTransferred = JSON(rawValue: value as! NSDictionary)
        self.delegate.callSegueFromCellUser(data: jsonTransferred!)
    } else {
        var jsonTransferred = JSON(rawValue: value as! NSArray)
        self.delegate.callSegueFromCellRecipients(data: jsonTransferred!)
    }
}
于 2015-06-26T12:33:52.207 回答
0

我是 ASDK 的主要维护者之一,很乐意帮助您解决任何挑战 — 随时打开项目的 GitHub 问题(即使只是提问)。

ASTextNode 缺少哪些你喜欢 TTT 的功能?它确实处理链接,并在多个、不相交和换行的链接之间完成基于质心的点击消歧。可能缺少一些功能,但由于该项目被广泛使用,我敢打赌其他开发人员会发现添加任何您需要的功能很有用。

也就是说,如果您不需要移动文本布局和渲染离开主线程带来的显着性能提升,您可以将 TTT 包装在 initWithViewBlock 中 - 或者直接在任何内部创建和添加视图,无需节点包装节点的 -didLoad 方法。通常使用 ASDK,不需要包装视图(这就是我询问 ASTextNode 的原因)。

祝你工作顺利!

于 2015-08-05T00:58:15.323 回答