0

我正在使用AsyncDisplayKit(第一次)并且有ASTextNode一个ASCellNode. 想在ASTextNode. 我试图用 a 包装它,ASDisplayNode但每当我计算它的大小时,calculateSizeThatFits:它总是返回 0。任何建议都将不胜感激。子类中的代码ASCellNode是:

- (CGSize)calculateSizeThatFits:(CGSize)constrainedSize
{
    CGSize textSize = [self.commentNode measure:CGSizeMake(constrainedSize.width - kImageSize - kImageToCommentPadding - kCellPadding - kInnerPadding, constrainedSize.height)];

    return CGSizeMake(constrainedSize.width, textSize.height);
}

- (void)layout
{
    self.imageNode.frame = CGRectMake(kCellPadding, kCellPadding, kImageSize, kImageSize);
    self.imageNode.layer.cornerRadius = kImageSize / 2.f;
    self.imageNode.layer.masksToBounds = YES;
    self.imageNode.layer.borderColor = [UIColor whiteColor].CGColor;
    self.imageNode.layer.borderWidth = 2.f;

    self.commentNode.backgroundColor = [UIColor whiteColor];
    self.commentNode.layer.cornerRadius = 8.f;
    self.commentNode.layer.masksToBounds = YES;

    CGSize textSize = self.commentNode.calculatedSize;
    self.commentNode.frame = CGRectMake(kCellPadding + kImageSize + kCellPadding, kCellPadding, textSize.width, textSize.height);
}
4

2 回答 2

2

如果您的节点返回 0 高度/大小,您可能会在更改其内容后忘记使其当前大小无效。

采用:[node invalidateCalculatedSize];

对于文本节点周围的填充,您可以在其后面添加一个具有所需大小的节点,然后hitTestSlop在文本节点上设置 a 。这将增加其可点击区域。

更好的方法可能是将其嵌入到自定义节点中,例如

界面

@interface InsetTextNode: ASControlNode

@property (nonatomic) UIEdgeInsets textInsets;
@property (nonatomic) NSAttributedString * attributedString;
@property ASTextNode * textNode;

@end

执行

@implementation InsetTextNode

- (instancetype)init
{
    self = [super init];
    if (!self) return nil;

    [self addSubnode:textNode];
    self.textInsets = UIEdgeInsetsMake(8, 16, 8, 16);

    return self;
}

- (CGSize)calculateSizeThatFits:(CGSize)constrainedSize
{
    CGFloat availableTextWidth = constrainedSize.width - self.textInsets.left - self.textInsets.right;
    CGFloat availableTextHeight = constrainedSize.height - self.textInsets.top - self.textInsets.bottom;
    CGSize constrainedTextSize = CGSizeMake(availableTextWidth, availableTextHeight);

    CGSize textSize = [self.textNode measure:constrainedTextSize];

    CGFloat finalWidth = self.textInsets.left + textSize.width + self.textInsets.right;
    CGFloat finalHeight = self.textInsets.top + textSize.height + self.textInsets.bottom;

    CGSize finalSize = CGSizeMake(finalWidth, finalHeight);

    return finalSize;
}

- (void)layout
{
    CGFloat textX = self.textInsets.left;
    CGFloat textY = self.textInsets.top;

    CGSize textSize = self.textNode.calculatedSize;

    textNode.frame = CGRectMake(textX, textY, textSize.width, textSize.height);
}

- (NSAttributedString *) attributedString
{
     return self.textNode.attributedString;
}

- (void)setAttributedString:(NSAttributedString *)attributedString
{
    self.textNode.attributedString = attributedString;
    [self invalidateCalculatedSize];
}

- (void)setTextInsets:(UIEdgeInsets)textInsets
{
    _textInsets = textInsets;

    [self invalidateCalculatedSize];
}

@end
于 2015-04-15T11:43:41.213 回答
2

2018:现在很容易实现相同的效果,所以以防万一有人需要在这里添加填充是一个 Swift 4 解决方案:-

   let messageLabel: ASTextNode = {
    let messageNode = ASTextNode()
    messageNode.textContainerInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    return messageNode
}()
于 2018-05-28T13:37:32.793 回答