Peter 9bc996374f Add 'submodules/AsyncDisplayKit/' from commit '02bedc12816e251ad71777f9d2578329b6d2bef6'
git-subtree-dir: submodules/AsyncDisplayKit
git-subtree-mainline: d06f423e0ed3df1fed9bd10d79ee312a9179b632
git-subtree-split: 02bedc12816e251ad71777f9d2578329b6d2bef6
2019-06-11 18:42:43 +01:00

163 lines
4.0 KiB
Objective-C

//
// ViewController.m
// Texture
//
// Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//
#import "ViewController.h"
#import <AsyncDisplayKit/AsyncDisplayKit.h>
#define NUMBER_ELEMENTS 2
@interface ViewController ()
{
NSMutableArray <ASTextNode *> *_textNodes;
NSMutableArray <UILabel *> *_textLabels;
UIScrollView *_scrollView;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_textNodes = [NSMutableArray array];
_textLabels = [NSMutableArray array];
_scrollView = [[UIScrollView alloc] init];
[self.view addSubview:_scrollView];
for (int i = 0; i < NUMBER_ELEMENTS; i++) {
ASTextNode *node = [self createNodeForIndex:i];
[_textNodes addObject:node];
[_scrollView addSubnode:node];
UILabel *label = [self createLabelForIndex:i];
[_textLabels addObject:label];
[_scrollView addSubview:label];
}
}
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
CGFloat maxWidth = 0;
CGFloat maxHeight = 0;
CGRect frame = CGRectMake(50, 50, 0, 0);
for (int i = 0; i < NUMBER_ELEMENTS; i++) {
frame.size = [self sizeForIndex:i];
[[_textNodes objectAtIndex:i] setFrame:frame];
frame.origin.x += frame.size.width + 50;
[[_textLabels objectAtIndex:i] setFrame:frame];
if (frame.size.width > maxWidth) {
maxWidth = frame.size.width;
}
if ((frame.size.height + frame.origin.y) > maxHeight) {
maxHeight = frame.size.height + frame.origin.y;
}
frame.origin.x -= frame.size.width + 50;
frame.origin.y += frame.size.height + 20;
}
_scrollView.frame = self.view.bounds;
_scrollView.contentSize = CGSizeMake(maxWidth, maxHeight);
}
- (ASTextNode *)createNodeForIndex:(NSUInteger)index
{
ASTextNode *node = [[ASTextNode alloc] init];
node.attributedText = [self textForIndex:index];
node.backgroundColor = [UIColor orangeColor];
NSMutableAttributedString *string = [node.attributedText mutableCopy];
switch (index) {
case 0: // top justification (ASDK) vs. center justification (UILabel)
node.maximumNumberOfLines = 3;
return node;
case 1: // default truncation attributed string color shouldn't match attributed text color (ASDK) vs. match (UIKit)
node.maximumNumberOfLines = 3;
[string addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(0, [string length])];
node.attributedText = string;
return node;
default:
return nil;
}
}
- (UILabel *)createLabelForIndex:(NSUInteger)index
{
UILabel *label = [[UILabel alloc] init];
label.attributedText = [self textForIndex:index];
label.backgroundColor = [UIColor greenColor];
NSMutableAttributedString *string = [label.attributedText mutableCopy];
switch (index) {
case 0:
label.numberOfLines = 3;
return label;
case 1:
label.numberOfLines = 3;
[string addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(0, [string length])];
label.attributedText = string;
return label;
default:
return nil;
}
}
- (NSAttributedString *)textForIndex:(NSUInteger)index
{
NSDictionary *attrs = @{ NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:12.0f] };
switch (index) {
case 0:
return [[NSAttributedString alloc] initWithString:@"1\n2\n3\n4\n5" attributes:attrs];
case 1:
return [[NSAttributedString alloc] initWithString:@"1\n2\n3\n4\n5" attributes:attrs];
default:
return nil;
}
}
- (CGSize)sizeForIndex:(NSUInteger)index
{
switch (index) {
case 0:
return CGSizeMake(40, 100);
case 1:
return CGSizeMake(40, 100);
default:
return CGSizeZero;
}
}
@end