2

私はを構築しましたがASCellNode、それは完全に機能します。ただし、トラディショナルUICollectionViewCellを使用したときはTTTAttributedLabel、リンク付きを使用しました。

これをどのように複製すればよいかわかりませんAsyncDisplayKit

attriubtedText を から に割り当てることはできますTTTAttributedLabelASTextNode、もちろんリンクは保持されません。どうすれば効率的にこれを行うことができますか。私の例を以下に示し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

私は ASDK の主要なメンテナーの 1 人であり、あらゆる課題への対処を支援したいと考えています。プロジェクトに関する GitHub の問題を自由に開いてください (質問だけでも)。

ASTextNode に欠けている TTT の好きな機能は何ですか? リンクを処理し、複数のばらばらな & 行折り返しリンク間の重心ベースのタップの曖昧さを解消します。おそらく不足している機能がありますが、このプロジェクトは非常に広く使用されているため、必要な機能を追加すると、他の開発者が便利になると思います。

とは言っても、テキスト レイアウトの移動とメイン スレッドからのレンダリングに伴う大幅なパフォーマンスの向上が必要ない場合は、TTT を initWithViewBlock でラップできます。ノードの -didLoad メソッド。通常、ASDK では、ビューをラップする必要はありません (これが、ASTextNode について尋ねた理由です)。

仕事頑張ってください!

于 2015-08-05T00:58:15.323 に答える
0

私は AsyncDisplayKit に精通していませんが、次の使用にはいくつかの問題がありますTTTAttributedLabel

  • TTTAttributedLabel()を呼び出す でラベルを初期化していますinit。配列やその他のさまざまな内部プロパティを適切に初期化しないため、代わりに指定された初期化子initWithFrame:またはinitWithCoder:を使用する必要があります。の最新リリースでは、は使用不可としてマークされています。initlinksTTTAttributedLabelinit

  • プロパティに割り当てていattributedTextます。次のメモを参照してくださいTTTAttributedLabel.h

    @bugattributedText以前に設定したリンクにアクセスしようとするとクラッシュする可能性があるため、直接設定することはお勧めしません。代わりに、 を呼び出しsetText:て を渡しますNSAttributedString

    attributedTextプロパティに割り当てないでください。

于 2015-06-19T18:42:13.750 に答える