SizeToFit AsyncDisplayKit

568 views Asked by At

My ASTextNod take normally 3 lines but i trancate it from the seconde line. Resultat: my ASTextNod show 2 lines and 3 points of trancate but his height is the height of 3 lines and not 2.

I look so for something similar to sizeToFit for ASTextNode.

1

There are 1 answers

0
Jonas Romain Wiesel On

It took me a while to get familiar with layouts in ASDisplayKit and there is very limited resources online. You need to use insetSpecs. But it would be easier if you could provide more details of how and where you are trying to display it.

Here is an exemple of a ASCellNode with text that has dynamic height and width within a width smaller than the screen size and a height of max 200 points.

class MyNode: ASCellNode {
     var myText: ASTextNode

    init() {
        myText = ASTextNode()
        addSubnode(myText)
    }

    override func layoutSpecThatFits(constrainedSize: ASSizeRange) -> ASLayoutSpec {

        let cellWidth = UIScreen.mainScreen().bounds.size.width
        myText.sizeRange = ASRelativeSizeRangeMake(
            ASRelativeSize(
                width: ASRelativeDimensionMakeWithPercent(0),
                height: ASRelativeDimensionMakeWithPoints(0)),
            ASRelativeSize(
                width: ASRelativeDimensionMakeWithPercent(cellWidth),
                height: ASRelativeDimensionMakeWithPoints(200)))

        let insetSpecs = ASInsetLayoutSpec(
            insets: UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8),
            child: myText)

        return insetSpecs
     }
 }