From 45c616d9166c01949f608ea3d39f1647b8e9dbb1 Mon Sep 17 00:00:00 2001 From: Michael Schneider Date: Thu, 25 Feb 2016 11:13:25 -0800 Subject: [PATCH] Improve ASTextCellNode customization --- AsyncDisplayKit/ASCellNode.h | 15 ++++ AsyncDisplayKit/ASCellNode.m | 76 ++++++++++++++----- .../Sample.xcodeproj/project.pbxproj | 6 -- .../Sample/SupplementaryNode.h | 18 ----- .../Sample/SupplementaryNode.m | 52 ------------- .../Sample/ViewController.m | 11 ++- 6 files changed, 80 insertions(+), 98 deletions(-) delete mode 100644 examples/CustomCollectionView/Sample/SupplementaryNode.h delete mode 100644 examples/CustomCollectionView/Sample/SupplementaryNode.m diff --git a/AsyncDisplayKit/ASCellNode.h b/AsyncDisplayKit/ASCellNode.h index fccc62997b..ccd62a317e 100644 --- a/AsyncDisplayKit/ASCellNode.h +++ b/AsyncDisplayKit/ASCellNode.h @@ -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 diff --git a/AsyncDisplayKit/ASCellNode.m b/AsyncDisplayKit/ASCellNode.m index a8a8b6f97f..0d528cff6f 100644 --- a/AsyncDisplayKit/ASCellNode.m +++ b/AsyncDisplayKit/ASCellNode.m @@ -9,6 +9,7 @@ #import "ASCellNode+Internal.h" #import "ASInternalHelpers.h" +#import "ASEqualityHelpers.h" #import #import #import @@ -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]; } diff --git a/examples/CustomCollectionView/Sample.xcodeproj/project.pbxproj b/examples/CustomCollectionView/Sample.xcodeproj/project.pbxproj index 88ca8858e3..70cc92d2d3 100644 --- a/examples/CustomCollectionView/Sample.xcodeproj/project.pbxproj +++ b/examples/CustomCollectionView/Sample.xcodeproj/project.pbxproj @@ -9,7 +9,6 @@ /* Begin PBXBuildFile section */ 25A1FA851C02F7AC00193875 /* MosaicCollectionViewLayout.m in Sources */ = {isa = PBXBuildFile; fileRef = 25A1FA841C02F7AC00193875 /* MosaicCollectionViewLayout.m */; }; 25A1FA881C02FCB000193875 /* ImageCellNode.m in Sources */ = {isa = PBXBuildFile; fileRef = 25A1FA871C02FCB000193875 /* ImageCellNode.m */; }; - 9B92C8811BC17D3000EE46B2 /* SupplementaryNode.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B92C8801BC17D3000EE46B2 /* SupplementaryNode.m */; }; 9BA2CEA11BB2579C00D18414 /* Launchboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9BA2CEA01BB2579C00D18414 /* Launchboard.storyboard */; }; AC3C4A641A11F47200143C57 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = AC3C4A631A11F47200143C57 /* main.m */; }; AC3C4A671A11F47200143C57 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = AC3C4A661A11F47200143C57 /* AppDelegate.m */; }; @@ -24,8 +23,6 @@ 25A1FA861C02FCB000193875 /* ImageCellNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ImageCellNode.h; sourceTree = ""; }; 25A1FA871C02FCB000193875 /* ImageCellNode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ImageCellNode.m; sourceTree = ""; }; 2DBAEE96397BB913350C4530 /* Pods.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.debug.xcconfig; path = "Pods/Target Support Files/Pods/Pods.debug.xcconfig"; sourceTree = ""; }; - 9B92C87F1BC17D3000EE46B2 /* SupplementaryNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SupplementaryNode.h; sourceTree = ""; }; - 9B92C8801BC17D3000EE46B2 /* SupplementaryNode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SupplementaryNode.m; sourceTree = ""; }; 9BA2CEA01BB2579C00D18414 /* Launchboard.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Launchboard.storyboard; sourceTree = ""; }; AC3C4A5E1A11F47200143C57 /* Sample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Sample.app; sourceTree = BUILT_PRODUCTS_DIR; }; AC3C4A621A11F47200143C57 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -91,8 +88,6 @@ 25A1FA871C02FCB000193875 /* ImageCellNode.m */, AC3C4A8D1A11F80C00143C57 /* Images.xcassets */, AC3C4A611A11F47200143C57 /* Supporting Files */, - 9B92C87F1BC17D3000EE46B2 /* SupplementaryNode.h */, - 9B92C8801BC17D3000EE46B2 /* SupplementaryNode.m */, ); indentWidth = 2; path = Sample; @@ -224,7 +219,6 @@ files = ( 25A1FA851C02F7AC00193875 /* MosaicCollectionViewLayout.m in Sources */, AC3C4A6A1A11F47200143C57 /* ViewController.m in Sources */, - 9B92C8811BC17D3000EE46B2 /* SupplementaryNode.m in Sources */, AC3C4A671A11F47200143C57 /* AppDelegate.m in Sources */, AC3C4A641A11F47200143C57 /* main.m in Sources */, 25A1FA881C02FCB000193875 /* ImageCellNode.m in Sources */, diff --git a/examples/CustomCollectionView/Sample/SupplementaryNode.h b/examples/CustomCollectionView/Sample/SupplementaryNode.h deleted file mode 100644 index f75c929684..0000000000 --- a/examples/CustomCollectionView/Sample/SupplementaryNode.h +++ /dev/null @@ -1,18 +0,0 @@ -/* This file provided by Facebook is for non-commercial testing and evaluation - * purposes only. Facebook reserves all rights not expressly granted. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#import - -@interface SupplementaryNode : ASCellNode - -- (instancetype)initWithText:(NSString *)text; - -@end diff --git a/examples/CustomCollectionView/Sample/SupplementaryNode.m b/examples/CustomCollectionView/Sample/SupplementaryNode.m deleted file mode 100644 index 76ba17b4b6..0000000000 --- a/examples/CustomCollectionView/Sample/SupplementaryNode.m +++ /dev/null @@ -1,52 +0,0 @@ -/* This file provided by Facebook is for non-commercial testing and evaluation - * purposes only. Facebook reserves all rights not expressly granted. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#import "SupplementaryNode.h" - -#import -#import -#import - -@implementation SupplementaryNode { - ASTextNode *_textNode; -} - -- (instancetype)initWithText:(NSString *)text -{ - self = [super init]; - if (self != nil) { - _textNode = [[ASTextNode alloc] init]; - _textNode.attributedString = [[NSAttributedString alloc] initWithString:text - attributes:[self textAttributes]]; - [self addSubnode:_textNode]; - } - return self; -} - -- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize -{ - ASCenterLayoutSpec *center = [[ASCenterLayoutSpec alloc] init]; - center.centeringOptions = ASCenterLayoutSpecCenteringY; - center.child = _textNode; - return center; -} - -#pragma mark - Text Formatting - -- (NSDictionary *)textAttributes -{ - return @{ - NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline], - NSForegroundColorAttributeName: [UIColor grayColor], - }; -} - -@end diff --git a/examples/CustomCollectionView/Sample/ViewController.m b/examples/CustomCollectionView/Sample/ViewController.m index f919f815fa..b3b4cd8703 100644 --- a/examples/CustomCollectionView/Sample/ViewController.m +++ b/examples/CustomCollectionView/Sample/ViewController.m @@ -13,7 +13,6 @@ #import #import "MosaicCollectionViewLayout.h" -#import "SupplementaryNode.h" #import "ImageCellNode.h" static NSUInteger kNumberOfImages = 14; @@ -107,8 +106,14 @@ static NSUInteger kNumberOfImages = 14; - (ASCellNode *)collectionView:(ASCollectionView *)collectionView nodeForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { - NSString *text = [NSString stringWithFormat:@"Section %zd", indexPath.section + 1]; - return [[SupplementaryNode alloc] initWithText:text]; + NSDictionary *textAttributes = @{ + NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline], + NSForegroundColorAttributeName: [UIColor grayColor] + }; + UIEdgeInsets textInsets = UIEdgeInsetsMake(11.0, 0, 11.0, 0); + ASTextCellNode *textCellNode = [[ASTextCellNode alloc] initWithAttributes:textAttributes insets:textInsets]; + textCellNode.text = [NSString stringWithFormat:@"Section %zd", indexPath.section + 1]; + return textCellNode; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView