Merge pull request #1185 from garrettmoon/addContentInsetToButtonNode

[ASButtonNode] Add support for contentInset and make it bit more threadsafe
This commit is contained in:
appleguy 2016-02-05 11:59:20 -08:00
commit 8abda67d1a
2 changed files with 72 additions and 9 deletions

View File

@ -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.
*

View File

@ -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