2

我的 ASTextNod 通常需要 3 行,但我从第二行转换它。结果:我的 ASTextNod 显示 2 行和 3 个 trancate 点,但他的高度是 3 行的高度而不是 2。

我为 ASTextNode 寻找类似于 sizeToFit 的东西。

4

1 回答 1

3

我花了一些时间来熟悉 ASDisplayKit 中的布局,并且在线资源非常有限。您需要使用 insetSpecs。但是,如果您可以提供更多有关您尝试显示它的方式和位置的详细信息,那就更容易了。

这是一个 ASCellNode 示例,其文本具有动态高度和宽度,宽度小于屏幕尺寸,高度最大为 200 点。

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
     }
 }
于 2016-02-17T15:29:19.380 回答