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; @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. * Returns the styled title associated with the specified state.
* *

View File

@ -11,6 +11,7 @@
#import "ASThread.h" #import "ASThread.h"
#import "ASDisplayNode+Subclasses.h" #import "ASDisplayNode+Subclasses.h"
#import "ASBackgroundLayoutSpec.h" #import "ASBackgroundLayoutSpec.h"
#import "ASInsetLayoutSpec.h"
@interface ASButtonNode () @interface ASButtonNode ()
{ {
@ -38,6 +39,9 @@
@synthesize contentSpacing = _contentSpacing; @synthesize contentSpacing = _contentSpacing;
@synthesize laysOutHorizontally = _laysOutHorizontally; @synthesize laysOutHorizontally = _laysOutHorizontally;
@synthesize contentVerticalAlignment = _contentVerticalAlignment;
@synthesize contentHorizontalAlignment = _contentHorizontalAlignment;
@synthesize contentEdgeInsets = _contentEdgeInsets;
- (instancetype)init - (instancetype)init
{ {
@ -54,8 +58,11 @@
[_imageNode setLayerBacked:YES]; [_imageNode setLayerBacked:YES];
[_backgroundImageNode setLayerBacked:YES]; [_backgroundImageNode setLayerBacked:YES];
[_titleNode setFlexShrink:YES];
_contentHorizontalAlignment = ASAlignmentMiddle; _contentHorizontalAlignment = ASAlignmentMiddle;
_contentVerticalAlignment = ASAlignmentCenter; _contentVerticalAlignment = ASAlignmentCenter;
_contentEdgeInsets = UIEdgeInsetsZero;
[self addSubnode:_backgroundImageNode]; [self addSubnode:_backgroundImageNode];
[self addSubnode:_titleNode]; [self addSubnode:_titleNode];
@ -196,6 +203,42 @@
[self setNeedsLayout]; [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 - (void)setTitle:(NSString *)title withFont:(UIFont *)font withColor:(UIColor *)color forState:(ASControlState)state
{ {
NSDictionary *attributes = @{ NSDictionary *attributes = @{
@ -352,12 +395,19 @@
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{ {
UIEdgeInsets contentEdgeInsets;
ASLayoutSpec *spec;
ASStackLayoutSpec *stack = [[ASStackLayoutSpec alloc] init]; ASStackLayoutSpec *stack = [[ASStackLayoutSpec alloc] init];
stack.direction = self.laysOutHorizontally ? ASStackLayoutDirectionHorizontal : ASStackLayoutDirectionVertical; {
stack.spacing = self.contentSpacing; ASDN::MutexLocker l(_propertyLock);
stack.direction = _laysOutHorizontally ? ASStackLayoutDirectionHorizontal : ASStackLayoutDirectionVertical;
stack.spacing = _contentSpacing;
stack.horizontalAlignment = _contentHorizontalAlignment; stack.horizontalAlignment = _contentHorizontalAlignment;
stack.verticalAlignment = _contentVerticalAlignment; stack.verticalAlignment = _contentVerticalAlignment;
contentEdgeInsets = _contentEdgeInsets;
}
NSMutableArray *children = [[NSMutableArray alloc] initWithCapacity:2]; NSMutableArray *children = [[NSMutableArray alloc] initWithCapacity:2];
if (self.imageNode.image) { if (self.imageNode.image) {
[children addObject:self.imageNode]; [children addObject:self.imageNode];
@ -369,12 +419,18 @@
stack.children = children; stack.children = children;
if (self.backgroundImageNode.image) { spec = stack;
return [ASBackgroundLayoutSpec backgroundLayoutSpecWithChild:stack
background:self.backgroundImageNode]; if (UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, contentEdgeInsets) == NO) {
} else { spec = [ASInsetLayoutSpec insetLayoutSpecWithInsets:contentEdgeInsets child:spec];
return stack;
} }
if (self.backgroundImageNode.image) {
spec = [ASBackgroundLayoutSpec backgroundLayoutSpecWithChild:spec
background:self.backgroundImageNode];
}
return spec;
} }
- (void)layout - (void)layout