ios - Add padding around ASTextNode -
i working asyncdisplaykit (for first time) , have astextnode
inside ascellnode
. want adding padding or inset around text inside astextnode
. attempted wrap asdisplaynode
whenever calculated it's size in calculatesizethatfits:
returned 0. suggestions appreciated. code in ascellnode
subclass is:
- (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 node returning 0 height / size, may forgetting invalidate current size after changing content.
use: [node invalidatecalculatedsize];
for padding around text node, add node behind size want , set hittestslop
on text node. increase tappable area.
the better approach might embed inside custom node e.g.
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