Files
AsyncDisplayKit/examples/ASLayoutSpecPlayground/Sample/LayoutExampleNodes.m
2016-09-18 22:26:36 -07:00

272 lines
10 KiB
Objective-C

//
// LayoutExampleNodes.m
// Sample
//
// Created by Hannah Troisi on 9/13/16.
// Copyright © 2016 Facebook. All rights reserved.
//
#import "LayoutExampleNodes.h"
#import "Utilities.h"
#import "UIImage+ASConvenience.h"
#define USER_IMAGE_HEIGHT 60
#define HORIZONTAL_BUFFER 10
#define VERTICAL_BUFFER 5
#define FONT_SIZE 20
@implementation LayoutExampleNode
- (instancetype)init
{
self = [super init];
if (self) {
self.usesImplicitHierarchyManagement = YES;
self.shouldVisualizeLayoutSpecs = NO;
self.shouldCacheLayoutSpec = YES;
self.backgroundColor = [UIColor whiteColor];
}
return self;
}
#pragma mark - helper methods
- (NSAttributedString *)usernameAttributedStringWithFontSize:(CGFloat)size
{
return [NSAttributedString attributedStringWithString:@"hannahmbanana"
fontSize:size
color:[UIColor darkBlueColor]
firstWordColor:nil];
}
- (NSAttributedString *)locationAttributedStringWithFontSize:(CGFloat)size
{
return [NSAttributedString attributedStringWithString:@"San Fransisco, CA"
fontSize:size
color:[UIColor lightBlueColor]
firstWordColor:nil];
}
- (NSAttributedString *)uploadDateAttributedStringWithFontSize:(CGFloat)size
{
return [NSAttributedString attributedStringWithString:@"30m"
fontSize:size
color:[UIColor lightGrayColor]
firstWordColor:nil];
}
- (NSAttributedString *)likesAttributedStringWithFontSize:(CGFloat)size
{
return [NSAttributedString attributedStringWithString:@"♥︎ 17 likes"
fontSize:size
color:[UIColor darkBlueColor]
firstWordColor:nil];
}
- (NSAttributedString *)descriptionAttributedStringWithFontSize:(CGFloat)size
{
NSString *string = [NSString stringWithFormat:@"hannahmbanana check out this cool pic from the internet!"];
NSAttributedString *attrString = [NSAttributedString attributedStringWithString:string
fontSize:size
color:[UIColor darkGrayColor]
firstWordColor:[UIColor darkBlueColor]];
return attrString;
}
@end
@implementation HorizontalStackWithSpacer
- (instancetype)init
{
self = [super init];
if (self) {
_usernameNode = [[ASTextNode alloc] init];
_usernameNode.attributedString = [self usernameAttributedStringWithFontSize:FONT_SIZE];
_postLocationNode = [[ASTextNode alloc] init];
_postLocationNode.maximumNumberOfLines = 1;
_postLocationNode.attributedString = [self locationAttributedStringWithFontSize:FONT_SIZE];
_postTimeNode = [[ASTextNode alloc] init];
_postTimeNode.attributedString = [self uploadDateAttributedStringWithFontSize:FONT_SIZE];
}
return self;
}
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
_usernameNode.flexShrink = YES;
_postLocationNode.flexShrink = YES;
ASStackLayoutSpec *verticalStackSpec = [ASStackLayoutSpec verticalStackLayoutSpec];
verticalStackSpec.flexShrink = YES;
// if fetching post location data from server, check if it is available yet
if (_postLocationNode.attributedString) {
[verticalStackSpec setChildren:@[_usernameNode, _postLocationNode]];
} else {
[verticalStackSpec setChildren:@[_usernameNode]];
}
ASLayoutSpec *spacerSpec = [[ASLayoutSpec alloc] init];
spacerSpec.flexGrow = YES;
spacerSpec.flexShrink = YES;
// horizontal stack
ASStackLayoutSpec *horizontalStackSpec = [ASStackLayoutSpec horizontalStackLayoutSpec];
horizontalStackSpec.alignItems = ASStackLayoutAlignItemsCenter; // center items vertically in horiz stack
horizontalStackSpec.justifyContent = ASStackLayoutJustifyContentStart; // justify content to left
horizontalStackSpec.flexShrink = YES;
horizontalStackSpec.flexGrow = YES;
[horizontalStackSpec setChildren:@[verticalStackSpec, spacerSpec, _postTimeNode]];
// inset horizontal stack
UIEdgeInsets insets = UIEdgeInsetsMake(0, 10, 0, 10);
ASInsetLayoutSpec *headerInsetSpec = [ASInsetLayoutSpec insetLayoutSpecWithInsets:insets child:horizontalStackSpec];
headerInsetSpec.flexShrink = YES;
headerInsetSpec.flexGrow = YES;
return headerInsetSpec;
}
@end
@implementation PhotoWithInsetTextOverlay
- (instancetype)init
{
self = [super init];
if (self) {
_photoNode = [[ASNetworkImageNode alloc] init];
_photoNode.URL = [NSURL URLWithString:@"http://asyncdisplaykit.org/static/images/layout-examples-photo-with-inset-text-overlay-photo.png"];
_titleNode = [[ASTextNode alloc] init];
_titleNode.maximumNumberOfLines = 2;
_titleNode.truncationAttributedString = [NSAttributedString attributedStringWithString:@"..."
fontSize:16
color:[UIColor whiteColor]
firstWordColor:nil];
_titleNode.attributedString = [NSAttributedString attributedStringWithString:@"family fall hikes"
fontSize:16
color:[UIColor whiteColor]
firstWordColor:nil];
}
return self;
}
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
_photoNode.preferredFrameSize = CGSizeMake(USER_IMAGE_HEIGHT*2, USER_IMAGE_HEIGHT*2);
ASStaticLayoutSpec *backgroundImageStaticSpec = [ASStaticLayoutSpec staticLayoutSpecWithChildren:@[_photoNode]];
UIEdgeInsets insets = UIEdgeInsetsMake(INFINITY, 12, 12, 12);
ASInsetLayoutSpec *textInsetSpec = [ASInsetLayoutSpec insetLayoutSpecWithInsets:insets child:_titleNode];
ASOverlayLayoutSpec *textOverlaySpec = [ASOverlayLayoutSpec overlayLayoutSpecWithChild:backgroundImageStaticSpec
overlay:textInsetSpec];
return textOverlaySpec;
}
@end
@implementation PhotoWithOutsetIconOverlay
- (instancetype)init
{
self = [super init];
if (self) {
_photoNode = [[ASNetworkImageNode alloc] init];
_photoNode.URL = [NSURL URLWithString:@"http://asyncdisplaykit.org/static/images/layout-examples-photo-with-outset-icon-overlay-photo.png"];
_iconNode = [[ASNetworkImageNode alloc] init];
_iconNode.URL = [NSURL URLWithString:@"http://asyncdisplaykit.org/static/images/layout-examples-photo-with-outset-icon-overlay-icon.png"];
[_iconNode setImageModificationBlock:^UIImage *(UIImage *image) { // FIXME: in framework autocomplete for setImageModificationBlock line seems broken
CGSize profileImageSize = CGSizeMake(USER_IMAGE_HEIGHT, USER_IMAGE_HEIGHT);
return [image makeCircularImageWithSize:profileImageSize withBorderWidth:10];
}];
}
return self;
}
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
_iconNode.preferredFrameSize = CGSizeMake(40, 40);
_photoNode.preferredFrameSize = CGSizeMake(150, 150);
CGFloat x = _photoNode.preferredFrameSize.width;
CGFloat y = 0;
_iconNode.layoutPosition = CGPointMake(x, y);
_photoNode.layoutPosition = CGPointMake(_iconNode.preferredFrameSize.height/2.0, _iconNode.preferredFrameSize.height/2.0);
ASStaticLayoutSpec *staticLayoutSpec = [ASStaticLayoutSpec staticLayoutSpecWithChildren:@[_photoNode, _iconNode]];
return staticLayoutSpec;
}
@end
@implementation FlexibleSeparatorSurroundingContent
- (instancetype)init
{
self = [super init];
if (self) {
self.backgroundColor = [UIColor cyanColor];
_topSeparator = [[ASImageNode alloc] init];
_topSeparator.image = [UIImage as_resizableRoundedImageWithCornerRadius:1.0
cornerColor:[UIColor blackColor]
fillColor:[UIColor blackColor]];
_textNode = [[ASTextNode alloc] init];
_textNode.attributedString = [NSAttributedString attributedStringWithString:@"this is a long text node"
fontSize:16
color:[UIColor blackColor]
firstWordColor:nil];
_bottomSeparator = [[ASImageNode alloc] init];
_bottomSeparator.image = [UIImage as_resizableRoundedImageWithCornerRadius:1.0
cornerColor:[UIColor blackColor]
fillColor:[UIColor blackColor]];
}
return self;
}
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
_topSeparator.flexGrow = YES;
_bottomSeparator.flexGrow = YES;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(10, 10, 10, 10);
ASInsetLayoutSpec *insetContentSpec = [ASInsetLayoutSpec insetLayoutSpecWithInsets:contentInsets
child:_textNode];
// final vertical stack
ASStackLayoutSpec *verticalStackSpec = [ASStackLayoutSpec verticalStackLayoutSpec];
verticalStackSpec.direction = ASStackLayoutDirectionVertical;
verticalStackSpec.justifyContent = ASStackLayoutJustifyContentCenter;
verticalStackSpec.alignItems = ASStackLayoutAlignItemsStretch;
verticalStackSpec.children = @[_topSeparator, insetContentSpec, _bottomSeparator];
return verticalStackSpec;
}
@end