diff --git a/examples/Kittens/Sample/AppDelegate.h b/examples/Kittens/Sample/AppDelegate.h index 2aa29369b4..85855277e9 100644 --- a/examples/Kittens/Sample/AppDelegate.h +++ b/examples/Kittens/Sample/AppDelegate.h @@ -11,6 +11,8 @@ #import +#define UseAutomaticLayout 1 + @interface AppDelegate : UIResponder @property (strong, nonatomic) UIWindow *window; diff --git a/examples/Kittens/Sample/BlurbNode.m b/examples/Kittens/Sample/BlurbNode.m index 62f0d37ec4..c26cecacad 100644 --- a/examples/Kittens/Sample/BlurbNode.m +++ b/examples/Kittens/Sample/BlurbNode.m @@ -10,6 +10,7 @@ */ #import "BlurbNode.h" +#import "AppDelegate.h" #import #import @@ -72,16 +73,37 @@ static NSString *kLinkAttributeName = @"PlaceKittenNodeLinkAttributeName"; [super didLoad]; } +#if UseAutomaticLayout - (id)layoutSpecThatFits:(ASSizeRange)constrainedSize { - return [ASInsetLayoutSpec - newWithInsets:UIEdgeInsetsMake(kTextPadding, kTextPadding, kTextPadding, kTextPadding) - child: - [ASCenterLayoutSpec - newWithCenteringOptions:ASCenterLayoutSpecCenteringX // Center the text horizontally - sizingOptions:ASCenterLayoutSpecSizingOptionMinimumY // Takes up minimum height - child:_textNode]]; + return + [ASInsetLayoutSpec + newWithInsets:UIEdgeInsetsMake(kTextPadding, kTextPadding, kTextPadding, kTextPadding) + child: + [ASCenterLayoutSpec + newWithCenteringOptions:ASCenterLayoutSpecCenteringX // Center the text horizontally + sizingOptions:ASCenterLayoutSpecSizingOptionMinimumY // Takes up minimum height + child:_textNode]]; } +#else +- (CGSize)calculateSizeThatFits:(CGSize)constrainedSize +{ + // called on a background thread. custom nodes must call -measure: on their subnodes in -calculateSizeThatFits: + CGSize measuredSize = [_textNode measure:CGSizeMake(constrainedSize.width - 2 * kTextPadding, + constrainedSize.height - 2 * kTextPadding)]; + return CGSizeMake(constrainedSize.width, measuredSize.height + 2 * kTextPadding); +} + +- (void)layout +{ + // called on the main thread. we'll use the stashed size from above, instead of blocking on text sizing + CGSize textNodeSize = _textNode.calculatedSize; + _textNode.frame = CGRectMake(roundf((self.calculatedSize.width - textNodeSize.width) / 2.0f), + kTextPadding, + textNodeSize.width, + textNodeSize.height); +} +#endif #pragma mark - #pragma mark ASTextNodeDelegate methods. diff --git a/examples/Kittens/Sample/KittenNode.mm b/examples/Kittens/Sample/KittenNode.mm index 09faf6cca6..db435d40ef 100644 --- a/examples/Kittens/Sample/KittenNode.mm +++ b/examples/Kittens/Sample/KittenNode.mm @@ -10,6 +10,7 @@ */ #import "KittenNode.h" +#import "AppDelegate.h" #import @@ -127,6 +128,7 @@ static const CGFloat kInnerPadding = 10.0f; NSParagraphStyleAttributeName: style }; } +#if UseAutomaticLayout - (id)layoutSpecThatFits:(ASSizeRange)constrainedSize { ASRatioLayoutSpec *imagePlaceholder = [ASRatioLayoutSpec newWithRatio:1.0 child:_imageNode]; @@ -146,12 +148,37 @@ static const CGFloat kInnerPadding = 10.0f; children:@[imagePlaceholder, _textNode]]]; } +// With box model, you don't need to override this method, unless you want to add custom logic. - (void)layout { [super layout]; + // Manually layout the divider. CGFloat pixelHeight = 1.0f / [[UIScreen mainScreen] scale]; _divider.frame = CGRectMake(0.0f, 0.0f, self.calculatedSize.width, pixelHeight); } +#else +- (CGSize)calculateSizeThatFits:(CGSize)constrainedSize +{ + CGSize imageSize = CGSizeMake(kImageSize, kImageSize); + CGSize textSize = [_textNode measure:CGSizeMake(constrainedSize.width - kImageSize - 2 * kOuterPadding - kInnerPadding, + constrainedSize.height)]; + + // ensure there's room for the text + CGFloat requiredHeight = MAX(textSize.height, imageSize.height); + return CGSizeMake(constrainedSize.width, requiredHeight + 2 * kOuterPadding); +} + +- (void)layout +{ + CGFloat pixelHeight = 1.0f / [[UIScreen mainScreen] scale]; + _divider.frame = CGRectMake(0.0f, 0.0f, self.calculatedSize.width, pixelHeight); + + _imageNode.frame = CGRectMake(kOuterPadding, kOuterPadding, kImageSize, kImageSize); + + CGSize textSize = _textNode.calculatedSize; + _textNode.frame = CGRectMake(kOuterPadding + kImageSize + kInnerPadding, kOuterPadding, textSize.width, textSize.height); +} +#endif @end