-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJSONLayout.m
More file actions
executable file
·188 lines (173 loc) · 6.06 KB
/
Copy pathJSONLayout.m
File metadata and controls
executable file
·188 lines (173 loc) · 6.06 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
//
// JSONLayout.m
// JSONViewExample
//
// Created by moises on 7/26/18.
// Copyright © 2018 pie33.com. All rights reserved.
//
#import "JSONLayout.h"
@implementation JSONLayout
- (instancetype) initWithDictionary:(NSDictionary *)dictionary {
if ((self = [super init])) {
[self setupSizeWithData:dictionary];
[self setupFlexingWithData:dictionary];
[self setupMarginsWithData:dictionary];
}
return self;
}
- (void) setupSizeWithData:(NSDictionary*)dictionary {
_width = ygValueFromValueObject(dictionary[@"width"]);
_height = ygValueFromValueObject(dictionary[@"height"]);
if (dictionary[@"min-width"]) {
_minWidth = ygValueFromValueObject(dictionary[@"min-width"]);
}
if (dictionary[@"min-height"]) {
_minHeight = ygValueFromValueObject(dictionary[@"min-height"]);
}
if (dictionary[@"max-width"]) {
_maxWidth = ygValueFromValueObject(dictionary[@"max-width"]);
}
if (dictionary[@"max-height"]) {
_maxHeight = ygValueFromValueObject(dictionary[@"max-height"]);
}
}
- (void) setupFlexingWithData:(NSDictionary*)dictionary {
/*
// TODO: direction
@property (nonatomic, readonly) YGDirection direction;
@property (nonatomic, readonly) YGPositionType position;
@property (nonatomic, readonly) YGOverflow overflow;
@property (nonatomic, readonly) YGDisplay display;
*/
// TODO: flex-flow shorthand for direction + wrap
_flexDirection = ygFlexDirectionFromString(dictionary[@"flex-direction"]);
_flexWrap = ygWrapFromString(dictionary[@"flex-wrap"]);
// TODO: flex shorthands (grow shrink basis, and initial auto none (some number))
_flexGrow = [dictionary[@"flex-grow"] doubleValue];
_flexShrink = [dictionary[@"flex-shrink"] doubleValue];
_flexBasis = ygValueFromValueObject(dictionary[@"flex-basis"]);
_alignContent = ygAlignFromString(dictionary[@"align-content"]);
_alignItems = ygAlignFromString(dictionary[@"align-items"]);
_alignSelf = ygAlignFromString(dictionary[@"align-self"]);
_justifyContent = ygJustifyFromString(dictionary[@"justify-content"]);
}
- (void) setupMarginsWithData:(NSDictionary*)dictionary {
if (dictionary[@"margin-left"]) {
_marginLeft = ygValueFromValueObject(dictionary[@"margin-left"]);
}
if (dictionary[@"margin-top"]) {
_marginTop = ygValueFromValueObject(dictionary[@"margin-top"]);
}
if (dictionary[@"margin-right"]) {
_marginRight = ygValueFromValueObject(dictionary[@"margin-right"]);
}
if (dictionary[@"margin-bottom"]) {
_marginBottom = ygValueFromValueObject(dictionary[@"margin-bottom"]);
}
if (dictionary[@"margin-start"]) {
_marginStart = ygValueFromValueObject(dictionary[@"margin-start"]);
}
if (dictionary[@"margin-end"]) {
_marginEnd = ygValueFromValueObject(dictionary[@"margin-end"]);
}
if (dictionary[@"margin-horizontal"]) {
_marginHorizontal = ygValueFromValueObject(dictionary[@"margin-horizontal"]);
}
if (dictionary[@"margin-vertical"]) {
_marginVertical = ygValueFromValueObject(dictionary[@"margin-vertical"]);
}
if (dictionary[@"margin"]) {
_margin = ygValueFromValueObject(dictionary[@"margin"]);
}
}
#pragma mark -
YGValue ygValueFromValueObject(NSObject *object) {
if ([object isKindOfClass:[NSNumber class]]) {
return YGPointValue(((NSNumber*)object).doubleValue);
}
else if ([object isKindOfClass:[NSString class]]) {
NSString *stringObject = (NSString*)object;
if ([stringObject rangeOfString:@"%"].location == NSNotFound) {
return YGPointValue(stringObject.doubleValue);
}
else {
return YGPercentValue(stringObject.doubleValue);
}
}
else {
return YGValueAuto;
}
}
YGFlexDirection ygFlexDirectionFromString(NSString* directionString) {
if ([directionString isEqualToString:@"column"]) {
return YGFlexDirectionColumn;
}
else if ([directionString isEqualToString:@"column-reverse"]) {
return YGFlexDirectionColumnReverse;
}
else if ([directionString isEqualToString:@"row"]) {
return YGFlexDirectionRow;
}
else if ([directionString isEqualToString:@"row-reverse"]) {
return YGFlexDirectionRowReverse;
}
else {
// default
return YGFlexDirectionColumn;
}
}
YGWrap ygWrapFromString(NSString* wrapString) {
if ([wrapString isEqualToString:@"wrap"]) {
return YGWrapWrap;
}
else if ([wrapString isEqualToString:@"wrap-reverse"]) {
return YGWrapWrapReverse;
}
return YGWrapNoWrap;
}
YGAlign ygAlignFromString(NSString* alignString) {
if ([alignString isEqualToString:@"flex-start"]) {
return YGAlignFlexStart;
}
else if ([alignString isEqualToString:@"center"]) {
return YGAlignCenter;
}
else if ([alignString isEqualToString:@"flex-end"]) {
return YGAlignFlexEnd;
}
else if ([alignString isEqualToString:@"stretch"]) {
return YGAlignStretch;
}
else if ([alignString isEqualToString:@"baseline"]) {
return YGAlignBaseline;
}
else if ([alignString isEqualToString:@"space-between"]) {
return YGAlignSpaceBetween;
}
else if ([alignString isEqualToString:@"space-around"]) {
return YGAlignSpaceAround;
}
return YGAlignAuto;
}
YGJustify ygJustifyFromString(NSString* justifyString) {
if ([justifyString isEqualToString:@"flex-start"]) {
return YGJustifyFlexStart;
}
else if ([justifyString isEqualToString:@"center"]) {
return YGJustifyCenter;
}
else if ([justifyString isEqualToString:@"flex-end"]) {
return YGJustifyFlexEnd;
}
else if ([justifyString isEqualToString:@"space-between"]) {
return YGJustifySpaceBetween;
}
else if ([justifyString isEqualToString:@"space-around"]) {
return YGJustifySpaceAround;
}
else if ([justifyString isEqualToString:@"space-evenly"]) {
return YGJustifySpaceEvenly;
}
return YGJustifyFlexStart;
}
@end