forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.m
More file actions
165 lines (126 loc) · 4.38 KB
/
Copy pathViewController.m
File metadata and controls
165 lines (126 loc) · 4.38 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
/* 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 "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