diff --git a/AsyncDisplayKit/ASButtonNode.h b/AsyncDisplayKit/ASButtonNode.h index 305121b3a6..89bd5ca245 100644 --- a/AsyncDisplayKit/ASButtonNode.h +++ b/AsyncDisplayKit/ASButtonNode.h @@ -37,6 +37,13 @@ */ @property (nonatomic, assign) ASVerticalAlignment contentVerticalAlignment; +/** + * @discussion insets the title and the image node + * + * @param contentEdgeInsets The insets used around the title and image node + */ +@property (nonatomic, assign) UIEdgeInsets contentEdgeInsets; + /** * Returns the styled title associated with the specified state. * diff --git a/AsyncDisplayKit/ASButtonNode.mm b/AsyncDisplayKit/ASButtonNode.mm index 7ed676e744..4831ba4fb3 100644 --- a/AsyncDisplayKit/ASButtonNode.mm +++ b/AsyncDisplayKit/ASButtonNode.mm @@ -11,6 +11,7 @@ #import "ASThread.h" #import "ASDisplayNode+Subclasses.h" #import "ASBackgroundLayoutSpec.h" +#import "ASInsetLayoutSpec.h" @interface ASButtonNode () { @@ -38,6 +39,9 @@ @synthesize contentSpacing = _contentSpacing; @synthesize laysOutHorizontally = _laysOutHorizontally; +@synthesize contentVerticalAlignment = _contentVerticalAlignment; +@synthesize contentHorizontalAlignment = _contentHorizontalAlignment; +@synthesize contentEdgeInsets = _contentEdgeInsets; - (instancetype)init { @@ -53,9 +57,12 @@ [_titleNode setLayerBacked:YES]; [_imageNode setLayerBacked:YES]; [_backgroundImageNode setLayerBacked:YES]; + + [_titleNode setFlexShrink:YES]; _contentHorizontalAlignment = ASAlignmentMiddle; _contentVerticalAlignment = ASAlignmentCenter; + _contentEdgeInsets = UIEdgeInsetsZero; [self addSubnode:_backgroundImageNode]; [self addSubnode:_titleNode]; @@ -196,6 +203,42 @@ [self setNeedsLayout]; } +- (ASVerticalAlignment)contentVerticalAlignment +{ + ASDN::MutexLocker l(_propertyLock); + return _contentVerticalAlignment; +} + +- (void)setContentVerticalAlignment:(ASVerticalAlignment)contentVerticalAlignment +{ + ASDN::MutexLocker l(_propertyLock); + _contentVerticalAlignment = contentVerticalAlignment; +} + +- (ASHorizontalAlignment)contentHorizontalAlignment +{ + ASDN::MutexLocker l(_propertyLock); + return _contentHorizontalAlignment; +} + +- (void)setContentHorizontalAlignment:(ASHorizontalAlignment)contentHorizontalAlignment +{ + ASDN::MutexLocker l(_propertyLock); + _contentHorizontalAlignment = contentHorizontalAlignment; +} + +- (UIEdgeInsets)contentEdgeInsets +{ + ASDN::MutexLocker l(_propertyLock); + return _contentEdgeInsets; +} + +- (void)setContentEdgeInsets:(UIEdgeInsets)contentEdgeInsets +{ + ASDN::MutexLocker l(_propertyLock); + _contentEdgeInsets = contentEdgeInsets; +} + - (void)setTitle:(NSString *)title withFont:(UIFont *)font withColor:(UIColor *)color forState:(ASControlState)state { NSDictionary *attributes = @{ @@ -352,11 +395,18 @@ - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize { + UIEdgeInsets contentEdgeInsets; + ASLayoutSpec *spec; ASStackLayoutSpec *stack = [[ASStackLayoutSpec alloc] init]; - stack.direction = self.laysOutHorizontally ? ASStackLayoutDirectionHorizontal : ASStackLayoutDirectionVertical; - stack.spacing = self.contentSpacing; - stack.horizontalAlignment = _contentHorizontalAlignment; - stack.verticalAlignment = _contentVerticalAlignment; + { + ASDN::MutexLocker l(_propertyLock); + stack.direction = _laysOutHorizontally ? ASStackLayoutDirectionHorizontal : ASStackLayoutDirectionVertical; + stack.spacing = _contentSpacing; + stack.horizontalAlignment = _contentHorizontalAlignment; + stack.verticalAlignment = _contentVerticalAlignment; + + contentEdgeInsets = _contentEdgeInsets; + } NSMutableArray *children = [[NSMutableArray alloc] initWithCapacity:2]; if (self.imageNode.image) { @@ -369,12 +419,18 @@ stack.children = children; - if (self.backgroundImageNode.image) { - return [ASBackgroundLayoutSpec backgroundLayoutSpecWithChild:stack - background:self.backgroundImageNode]; - } else { - return stack; + spec = stack; + + if (UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, contentEdgeInsets) == NO) { + spec = [ASInsetLayoutSpec insetLayoutSpecWithInsets:contentEdgeInsets child:spec]; } + + if (self.backgroundImageNode.image) { + spec = [ASBackgroundLayoutSpec backgroundLayoutSpecWithChild:spec + background:self.backgroundImageNode]; + } + + return spec; } - (void)layout