AsyncDisplayKit icon indicating copy to clipboard operation
AsyncDisplayKit copied to clipboard

[ASTextNode] support insets

Open Yue-Wang-Google opened this issue 10 years ago • 7 comments

Unlike textview / label, textnode does not have insets/margins. It would be great to support this feature.

Yue-Wang-Google avatar Jul 27 '16 23:07 Yue-Wang-Google

#2000! Yay!

Yue-Wang-Google avatar Jul 27 '16 23:07 Yue-Wang-Google

Until it gets added to the master branch, you can use this (just create a new file in your project):

// ASCustomTextNode
// Workaround for ASTextNode with padding (as in CSS)
//
//  -------------
// |  ---------  <-- UIEdgeInsets
// | |titleNode| |
// |  ---------  | <-- ASControlNode
//  -------------

import AsyncDisplayKit

class ASCustomTextNode: ASControlNode {

    var titleNode: ASTextNode!
    var insets: UIEdgeInsets!

    init(titleNode: ASTextNode, insets: UIEdgeInsets) {

        log.debug("ASCustomTextNode init")
        log.debug("titleNode = \(titleNode)")
        self.titleNode = titleNode
        titleNode.layerBacked = true

        self.insets = insets

        super.init()

        addSubnode(titleNode)
    }

    override func layoutSpecThatFits(constrainedSize: ASSizeRange) -> ASLayoutSpec {
        return ASStackLayoutSpec(
            direction: .Horizontal,
            spacing: 0,
            justifyContent: .Center,
            alignItems: .Center,
            children: [
                ASInsetLayoutSpec(
                    insets: insets,
                    child: titleNode
                )
            ]
        )
    }

}

It works perfectly!

benjaminhorner avatar Jul 28 '16 14:07 benjaminhorner

I'm not sure if you need a stack layout spec for that at all. Just wrap it in a inset spec or @benjaminhorner any reason you use a stack layout spec in this case?

maicki avatar Jul 28 '16 14:07 maicki

@maicki: you are totally right. I am actually using it in a larger file. I just extracted that bit. However, as you point out, returning an inset spec is a better solution.

benjaminhorner avatar Jul 28 '16 15:07 benjaminhorner

this issue is related to #1999, since it requires more complex fix, having insets may give a short-term solution. Sorry @maichi and @benjaminhorner , extending it with UIEdgeInsets won't fix that particular bug.

Yue-Wang-Google avatar Jul 28 '16 17:07 Yue-Wang-Google

I spoke with @Yue-Wang-Google and understand the core issue here - let's try to fix the italics bug first, but adding this as a +Beta API as a workaround could be reasonable too!

appleguy avatar Aug 03 '16 20:08 appleguy

Thanks @garrettmoon for adding this to ASTextNode!

@Yue-Wang-Google try this out and let us know if this doesn't work for you. The fix is currently in master (2.0 beta), which contains a few minor breaking API changes. I'll try to get this into 1.9.91 as well so that you can test it when we bump the pod in a day or two.

hannahmbanana avatar Aug 14 '16 19:08 hannahmbanana