forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayoutExampleNodes.m
More file actions
468 lines (357 loc) · 16.5 KB
/
Copy pathLayoutExampleNodes.m
File metadata and controls
468 lines (357 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//
// LayoutExampleNodes.m
// Texture
//
// Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
// Changes after 4/13/2017 are: Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//
#import "LayoutExampleNodes.h"
#import <AsyncDisplayKit/UIImage+ASConvenience.h>
#import "Utilities.h"
@interface HeaderWithRightAndLeftItems ()
@property (nonatomic, strong) ASTextNode *usernameNode;
@property (nonatomic, strong) ASTextNode *postLocationNode;
@property (nonatomic, strong) ASTextNode *postTimeNode;
@end
@interface PhotoWithInsetTextOverlay ()
@property (nonatomic, strong) ASNetworkImageNode *photoNode;
@property (nonatomic, strong) ASTextNode *titleNode;
@end
@interface PhotoWithOutsetIconOverlay ()
@property (nonatomic, strong) ASNetworkImageNode *photoNode;
@property (nonatomic, strong) ASNetworkImageNode *iconNode;
@end
@interface FlexibleSeparatorSurroundingContent ()
@property (nonatomic, strong) ASImageNode *topSeparator;
@property (nonatomic, strong) ASImageNode *bottomSeparator;
@property (nonatomic, strong) ASTextNode *textNode;
@end
@implementation HeaderWithRightAndLeftItems
+ (NSString *)title
{
return @"Header with left and right justified text";
}
+ (NSString *)descriptionTitle
{
return @"try rotating me!";
}
- (instancetype)init
{
self = [super init];
if (self) {
_usernameNode = [[ASTextNode alloc] init];
_usernameNode.attributedText = [NSAttributedString attributedStringWithString:@"hannahmbanana"
fontSize:20
color:[UIColor darkBlueColor]];
_usernameNode.maximumNumberOfLines = 1;
_usernameNode.truncationMode = NSLineBreakByTruncatingTail;
_postLocationNode = [[ASTextNode alloc] init];
_postLocationNode.maximumNumberOfLines = 1;
_postLocationNode.attributedText = [NSAttributedString attributedStringWithString:@"Sunset Beach, San Fransisco, CA"
fontSize:20
color:[UIColor lightBlueColor]];
_postLocationNode.maximumNumberOfLines = 1;
_postLocationNode.truncationMode = NSLineBreakByTruncatingTail;
_postTimeNode = [[ASTextNode alloc] init];
_postTimeNode.attributedText = [NSAttributedString attributedStringWithString:@"30m"
fontSize:20
color:[UIColor lightGrayColor]];
_postLocationNode.maximumNumberOfLines = 1;
_postLocationNode.truncationMode = NSLineBreakByTruncatingTail;
}
return self;
}
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
ASStackLayoutSpec *nameLocationStack = [ASStackLayoutSpec verticalStackLayoutSpec];
nameLocationStack.style.flexShrink = 1.0;
nameLocationStack.style.flexGrow = 1.0;
if (_postLocationNode.attributedText) {
nameLocationStack.children = @[_usernameNode, _postLocationNode];
} else {
nameLocationStack.children = @[_usernameNode];
}
ASStackLayoutSpec *headerStackSpec = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal
spacing:40
justifyContent:ASStackLayoutJustifyContentStart
alignItems:ASStackLayoutAlignItemsCenter
children:@[nameLocationStack, _postTimeNode]];
return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(0, 10, 0, 10) child:headerStackSpec];
}
@end
@implementation PhotoWithInsetTextOverlay
+ (NSString *)title
{
return @"Photo with inset text overlay";
}
+ (NSString *)descriptionTitle
{
return @"try rotating me!";
}
- (instancetype)init
{
self = [super init];
if (self) {
self.backgroundColor = [UIColor clearColor];
_photoNode = [[ASNetworkImageNode alloc] init];
_photoNode.URL = [NSURL URLWithString:@"http://texturegroup.org/static/images/layout-examples-photo-with-inset-text-overlay-photo.png"];
_photoNode.willDisplayNodeContentWithRenderingContext = ^(CGContextRef context, id drawParameters) {
CGRect bounds = CGContextGetClipBoundingBox(context);
[[UIBezierPath bezierPathWithRoundedRect:bounds cornerRadius:10] addClip];
};
_titleNode = [[ASTextNode alloc] init];
_titleNode.maximumNumberOfLines = 2;
_titleNode.truncationMode = NSLineBreakByTruncatingTail;
_titleNode.truncationAttributedText = [NSAttributedString attributedStringWithString:@"..." fontSize:16 color:[UIColor whiteColor]];
_titleNode.attributedText = [NSAttributedString attributedStringWithString:@"family fall hikes" fontSize:16 color:[UIColor whiteColor]];
}
return self;
}
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
CGFloat photoDimension = constrainedSize.max.width / 4.0;
_photoNode.style.preferredSize = CGSizeMake(photoDimension, photoDimension);
// INFINITY is used to make the inset unbounded
UIEdgeInsets insets = UIEdgeInsetsMake(INFINITY, 12, 12, 12);
ASInsetLayoutSpec *textInsetSpec = [ASInsetLayoutSpec insetLayoutSpecWithInsets:insets child:_titleNode];
return [ASOverlayLayoutSpec overlayLayoutSpecWithChild:_photoNode overlay:textInsetSpec];;
}
@end
@implementation PhotoWithOutsetIconOverlay
+ (NSString *)title
{
return @"Photo with outset icon overlay";
}
- (instancetype)init
{
self = [super init];
if (self) {
_photoNode = [[ASNetworkImageNode alloc] init];
_photoNode.URL = [NSURL URLWithString:@"http://texturegroup.org/static/images/layout-examples-photo-with-outset-icon-overlay-photo.png"];
_iconNode = [[ASNetworkImageNode alloc] init];
_iconNode.URL = [NSURL URLWithString:@"http://texturegroup.org/static/images/layout-examples-photo-with-outset-icon-overlay-icon.png"];
[_iconNode setImageModificationBlock:^UIImage *(UIImage *image, ASPrimitiveTraitCollection traitCollection) { // FIXME: in framework autocomplete for setImageModificationBlock line seems broken
CGSize profileImageSize = CGSizeMake(60, 60);
return [image makeCircularImageWithSize:profileImageSize withBorderWidth:10];
}];
}
return self;
}
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
_iconNode.style.preferredSize = CGSizeMake(40, 40);
_iconNode.style.layoutPosition = CGPointMake(150, 0);
_photoNode.style.preferredSize = CGSizeMake(150, 150);
_photoNode.style.layoutPosition = CGPointMake(40 / 2.0, 40 / 2.0);
ASAbsoluteLayoutSpec *absoluteSpec = [ASAbsoluteLayoutSpec absoluteLayoutSpecWithChildren:@[_photoNode, _iconNode]];
// ASAbsoluteLayoutSpec's .sizing property recreates the behavior of ASDK Layout API 1.0's "ASStaticLayoutSpec"
absoluteSpec.sizing = ASAbsoluteLayoutSpecSizingSizeToFit;
return absoluteSpec;
}
@end
@implementation FlexibleSeparatorSurroundingContent
+ (NSString *)title
{
return @"Top and bottom cell separator lines";
}
+ (NSString *)descriptionTitle
{
return @"try rotating me!";
}
- (instancetype)init
{
self = [super init];
if (self) {
self.backgroundColor = [UIColor whiteColor];
_topSeparator = [[ASImageNode alloc] init];
_topSeparator.image = [UIImage as_resizableRoundedImageWithCornerRadius:1.0
cornerColor:[UIColor blackColor]
fillColor:[UIColor blackColor]
traitCollection:self.primitiveTraitCollection];
_textNode = [[ASTextNode alloc] init];
_textNode.attributedText = [NSAttributedString attributedStringWithString:@"this is a long text node"
fontSize:16
color:[UIColor blackColor]];
_bottomSeparator = [[ASImageNode alloc] init];
_bottomSeparator.image = [UIImage as_resizableRoundedImageWithCornerRadius:1.0
cornerColor:[UIColor blackColor]
fillColor:[UIColor blackColor]
traitCollection:self.primitiveTraitCollection];
}
return self;
}
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
_topSeparator.style.flexGrow = 1.0;
_bottomSeparator.style.flexGrow = 1.0;
_textNode.style.alignSelf = ASStackLayoutAlignSelfCenter;
ASStackLayoutSpec *verticalStackSpec = [ASStackLayoutSpec verticalStackLayoutSpec];
verticalStackSpec.spacing = 20;
verticalStackSpec.justifyContent = ASStackLayoutJustifyContentCenter;
verticalStackSpec.children = @[_topSeparator, _textNode, _bottomSeparator];
return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(60, 0, 60, 0) child:verticalStackSpec];
}
@end
@interface CornerLayoutExample ()
@property (nonatomic, strong) ASImageNode *dotNode;
@property (nonatomic, strong) ASImageNode *photoNode1;
@property (nonatomic, strong) ASTextNode *badgeTextNode;
@property (nonatomic, strong) ASImageNode *badgeImageNode;
@property (nonatomic, strong) ASImageNode *photoNode2;
@end
@implementation CornerLayoutExample
static CGFloat const kSampleAvatarSize = 100;
static CGFloat const kSampleIconSize = 26;
static CGFloat const kSampleBadgeCornerRadius = 12;
+ (NSString *)title
{
return @"Declarative way for Corner image Layout";
}
+ (NSString *)descriptionTitle
{
return nil;
}
- (instancetype)init
{
self = [super init];
if (self) {
UIImage *avatarImage = [self avatarImageWithSize:CGSizeMake(kSampleAvatarSize, kSampleAvatarSize)];
UIImage *cornerImage = [self cornerImageWithSize:CGSizeMake(kSampleIconSize, kSampleIconSize)];
NSAttributedString *numberText = [NSAttributedString attributedStringWithString:@" 999+ " fontSize:20 color:UIColor.whiteColor];
_dotNode = [ASImageNode new];
_dotNode.image = cornerImage;
_photoNode1 = [ASImageNode new];
_photoNode1.image = avatarImage;
_badgeTextNode = [ASTextNode new];
_badgeTextNode.attributedText = numberText;
_badgeImageNode = [ASImageNode new];
_badgeImageNode.image = [UIImage as_resizableRoundedImageWithCornerRadius:kSampleBadgeCornerRadius
cornerColor:UIColor.clearColor
fillColor:UIColor.redColor
traitCollection:self.primitiveTraitCollection];
_photoNode2 = [ASImageNode new];
_photoNode2.image = avatarImage;
}
return self;
}
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
ASBackgroundLayoutSpec *badgeSpec = [ASBackgroundLayoutSpec backgroundLayoutSpecWithChild:_badgeTextNode
background:_badgeImageNode];
ASCornerLayoutSpec *cornerSpec1 = [ASCornerLayoutSpec cornerLayoutSpecWithChild:_photoNode1 corner:_dotNode location:ASCornerLayoutLocationTopRight];
cornerSpec1.offset = CGPointMake(-3, 3);
ASCornerLayoutSpec *cornerSpec2 = [ASCornerLayoutSpec cornerLayoutSpecWithChild:_photoNode2 corner:badgeSpec location:ASCornerLayoutLocationTopRight];
self.photoNode.style.preferredSize = CGSizeMake(kSampleAvatarSize, kSampleAvatarSize);
self.iconNode.style.preferredSize = CGSizeMake(kSampleIconSize, kSampleIconSize);
ASCornerLayoutSpec *cornerSpec3 = [ASCornerLayoutSpec cornerLayoutSpecWithChild:self.photoNode corner:self.iconNode location:ASCornerLayoutLocationTopRight];
ASStackLayoutSpec *stackSpec = [ASStackLayoutSpec verticalStackLayoutSpec];
stackSpec.spacing = 40;
stackSpec.children = @[cornerSpec1, cornerSpec2, cornerSpec3];
return stackSpec;
}
- (UIImage *)avatarImageWithSize:(CGSize)size
{
return [UIImage imageWithSize:size fillColor:UIColor.lightGrayColor shapeBlock:^UIBezierPath *{
CGRect rect = (CGRect){ CGPointZero, size };
return [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:MIN(size.width, size.height) / 20];
}];
}
- (UIImage *)cornerImageWithSize:(CGSize)size
{
return [UIImage imageWithSize:size fillColor:UIColor.redColor shapeBlock:^UIBezierPath *{
return [UIBezierPath bezierPathWithOvalInRect:(CGRect){ CGPointZero, size }];
}];
}
@end
@interface UserProfileSample ()
@property (nonatomic, strong) ASImageNode *badgeNode;
@property (nonatomic, strong) ASImageNode *avatarNode;
@property (nonatomic, strong) ASTextNode *usernameNode;
@property (nonatomic, strong) ASTextNode *subtitleNode;
@property (nonatomic, assign) CGFloat photoSizeValue;
@property (nonatomic, assign) CGFloat iconSizeValue;
@end
@implementation UserProfileSample
+ (NSString *)title
{
return @"Common user profile layout.";
}
+ (NSString *)descriptionTitle
{
return @"For corner image layout and text truncation.";
}
- (instancetype)init
{
self = [super init];
if (self) {
_photoSizeValue = 44;
_iconSizeValue = 15;
CGSize iconSize = CGSizeMake(_iconSizeValue, _iconSizeValue);
CGSize photoSize = CGSizeMake(_photoSizeValue, _photoSizeValue);
_badgeNode = [ASImageNode new];
_badgeNode.style.preferredSize = iconSize;
_badgeNode.image = [UIImage imageWithSize:iconSize fillColor:UIColor.redColor shapeBlock:^UIBezierPath *{
return [UIBezierPath bezierPathWithOvalInRect:(CGRect){ CGPointZero, iconSize }];
}];
_avatarNode = [ASImageNode new];
_avatarNode.style.preferredSize = photoSize;
_avatarNode.image = [UIImage imageWithSize:photoSize fillColor:UIColor.lightGrayColor shapeBlock:^UIBezierPath *{
return [UIBezierPath bezierPathWithOvalInRect:(CGRect){ CGPointZero, photoSize }];
}];
_usernameNode = [ASTextNode new];
_usernameNode.attributedText = [NSAttributedString attributedStringWithString:@"Hello World" fontSize:17 color:UIColor.blackColor];
_usernameNode.maximumNumberOfLines = 1;
_subtitleNode = [ASTextNode new];
_subtitleNode.attributedText = [NSAttributedString attributedStringWithString:@"This is a long long subtitle, with a long long appended string." fontSize:14 color:UIColor.lightGrayColor];
_subtitleNode.maximumNumberOfLines = 1;
}
return self;
}
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
// Apply avatar with badge
// Normally, avatar's box size is the only photo size and it will not include the badge size.
// Otherwise, use includeCornerForSizeCalculation property to increase the box's size if needed.
ASCornerLayoutSpec *avatarBox = [ASCornerLayoutSpec new];
avatarBox.child = _avatarNode;
avatarBox.corner = _badgeNode;
avatarBox.cornerLocation = ASCornerLayoutLocationBottomRight;
avatarBox.offset = CGPointMake(-6, -6);
ASStackLayoutSpec *textBox = [ASStackLayoutSpec verticalStackLayoutSpec];
textBox.justifyContent = ASStackLayoutJustifyContentSpaceAround;
textBox.children = @[_usernameNode, _subtitleNode];
ASStackLayoutSpec *profileBox = [ASStackLayoutSpec horizontalStackLayoutSpec];
profileBox.spacing = 10;
profileBox.children = @[avatarBox, textBox];
// Apply text truncation.
NSArray *elems = @[_usernameNode, _subtitleNode, textBox, profileBox];
for (id <ASLayoutElement> elem in elems) {
elem.style.flexShrink = 1;
}
ASInsetLayoutSpec *profileInsetBox = [ASInsetLayoutSpec new];
profileInsetBox.insets = UIEdgeInsetsMake(120, 20, INFINITY, 20);
profileInsetBox.child = profileBox;
return profileInsetBox;
}
@end
@implementation LayoutExampleNode
+ (NSString *)title
{
NSAssert(NO, @"All layout example nodes must provide a title!");
return nil;
}
+ (NSString *)descriptionTitle
{
return nil;
}
- (instancetype)init
{
self = [super init];
if (self) {
self.automaticallyManagesSubnodes = YES;
self.backgroundColor = [UIColor whiteColor];
}
return self;
}
@end