Improve ASTextCellNode customization

This commit is contained in:
Michael Schneider
2016-02-25 11:13:25 -08:00
parent a3576d5d61
commit 45c616d916
6 changed files with 80 additions and 98 deletions

View File

@@ -100,11 +100,26 @@ typedef NSUInteger ASCellNodeAnimation;
*/
@interface ASTextCellNode : ASCellNode
/**
* Initializes a text cell with given text attributes and text insets
*/
- (instancetype)initWithAttributes:(NSDictionary *)textAttributes insets:(UIEdgeInsets)textInsets;
/**
* Text to display.
*/
@property (nonatomic, copy) NSString *text;
/**
* A dictionary containing key-value pairs for text attributes. You can specify the font, text color, text shadow color, and text shadow offset using the keys listed in NSString UIKit Additions Reference.
*/
@property (nonatomic, copy) NSDictionary *textAttributes;
/**
* The text inset or outset for each edge. The default value is 15.0 horizontal and 11.0 vertical padding.
*/
@property (nonatomic, assign) UIEdgeInsets textInsets;
@end
NS_ASSUME_NONNULL_END

View File

@@ -9,6 +9,7 @@
#import "ASCellNode+Internal.h"
#import "ASInternalHelpers.h"
#import "ASEqualityHelpers.h"
#import <AsyncDisplayKit/_ASDisplayView.h>
#import <AsyncDisplayKit/ASDisplayNode+Subclasses.h>
#import <AsyncDisplayKit/ASDisplayNode+Beta.h>
@@ -190,46 +191,83 @@
#pragma mark ASTextCellNode
@interface ASTextCellNode ()
{
NSString *_text;
ASTextNode *_textNode;
}
@property (nonatomic, strong) ASTextNode *textNode;
@end
@implementation ASTextCellNode
static const CGFloat kFontSize = 18.0f;
static const CGFloat kASTextCellNodeDefaultFontSize = 18.0f;
static const CGFloat kASTextCellNodeDefaultHorizontalPadding = 15.0f;
static const CGFloat kASTextCellNodeDefaultVerticalPadding = 11.0f;
- (instancetype)init
{
if (!(self = [super init]))
return nil;
_text = @"";
_textNode = [[ASTextNode alloc] init];
[self addSubnode:_textNode];
return [self initWithAttributes:[self defaultTextAttributes] insets:[self defaultTextInsets]];
}
- (instancetype)initWithAttributes:(NSDictionary *)textAttributes insets:(UIEdgeInsets)textInsets
{
self = [super init];
if (self) {
_textInsets = textInsets;
_textAttributes = [textAttributes copy];
_textNode = [[ASTextNode alloc] init];
[self addSubnode:_textNode];
}
return self;
}
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
static const CGFloat kHorizontalPadding = 15.0f;
static const CGFloat kVerticalPadding = 11.0f;
UIEdgeInsets insets = UIEdgeInsetsMake(kVerticalPadding, kHorizontalPadding, kVerticalPadding, kHorizontalPadding);
return [ASInsetLayoutSpec insetLayoutSpecWithInsets:insets child:_textNode];
return [ASInsetLayoutSpec insetLayoutSpecWithInsets:self.textInsets child:self.textNode];
}
- (NSDictionary *)defaultTextAttributes
{
return @{NSFontAttributeName : [UIFont systemFontOfSize:kASTextCellNodeDefaultFontSize]};
}
- (UIEdgeInsets)defaultTextInsets
{
return UIEdgeInsetsMake(kASTextCellNodeDefaultVerticalPadding, kASTextCellNodeDefaultHorizontalPadding, kASTextCellNodeDefaultVerticalPadding, kASTextCellNodeDefaultHorizontalPadding);
}
- (void)setTextAttributes:(NSDictionary *)textAttributes
{
ASDisplayNodeAssertNotNil(textAttributes, @"Invalid text attributes");
_textAttributes = [textAttributes copy];
[self updateAttributedString];
}
- (void)setTextInsets:(UIEdgeInsets)textInsets
{
_textInsets = textInsets;
[self updateAttributedString];
}
- (void)setText:(NSString *)text
{
if (_text == text)
return;
if (ASObjectIsEqual(_text, text)) return;
_text = [text copy];
_textNode.attributedString = [[NSAttributedString alloc] initWithString:_text
attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:kFontSize]}];
[self updateAttributedString];
}
- (void)updateAttributedString
{
if (_text == nil) {
_textNode.attributedString = nil;
return;
}
_textNode.attributedString = [[NSAttributedString alloc] initWithString:self.text attributes:self.textAttributes];
[self setNeedsLayout];
}