简体   繁体   English

在ASTextNode周围添加填充

[英]Add padding around ASTextNode

I am working with AsyncDisplayKit (for the first time) and have an ASTextNode inside a ASCellNode . 我正在使用AsyncDisplayKit (第一次),并且在ASTextNode具有ASCellNode I want to adding padding or an inset around the text inside the ASTextNode . 我想在ASTextNode内部的文本周围添加填充或插图 I attempted to wrap it with a ASDisplayNode but whenever I calculated it's size in calculateSizeThatFits: it always returned 0. Any suggestions would be appreciated. 我试图用ASDisplayNode包装它,但是每当我在calculateSizeThatFits:计算它的大小时,它总是返回0。任何建议将不胜感激。 The code that is in the ASCellNode subclass is: 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);
}

If you node is returning 0 height / size, you may be forgetting to invalidate its current size after changing its content. 如果节点返回的高度/大小为0,则更改其内容后可能会忘记使其当前大小无效。

Use: [node invalidateCalculatedSize]; 使用: [node invalidateCalculatedSize];

For padding around the text node, you could add a node behind it with the size you want and then set a hitTestSlop on the text node. 要在文本节点周围进行填充,可以在其后添加所需大小的节点,然后在文本节点上设置hitTestSlop This would increase its tappable area. 这将增加其可点击的面积。

The better approach might be to embed it inside a custom node eg 更好的方法可能是将其嵌入自定义节点中,例如

Interface 接口

@interface InsetTextNode: ASControlNode

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

@end

Implementation 履行

@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

2018: Now it is super easy to achieve the same effect, So just in case someone needs to add padding here is a Swift 4 solution: - 2018年:现在实现相同的效果非常容易,因此,万一有人需要在此处添加填充是Swift 4解决方案:-

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM