在ASTextNode周围添加填充

时间:2015-04-09 21:54:18

标签: ios asyncdisplaykit

我正在使用 AsyncDisplayKit (第一次)并在ASTextNode内部有一个ASCellNode。我想在ASTextNode内的文本周围添加填充或插入。我尝试用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);
}

2 个答案:

答案 0 :(得分:2)

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

使用:[node invalidateCalculatedSize];

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

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

接口

@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

答案 1 :(得分:2)

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

   let messageLabel: ASTextNode = {
    let messageNode = ASTextNode()
    messageNode.textContainerInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    return messageNode
}()