forked from wix/react-native-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRCTHelpers.m
198 lines (159 loc) · 6.76 KB
/
RCTHelpers.m
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
#import "RCTHelpers.h"
#import <React/RCTView.h>
#import <React/RCTScrollView.h>
#import <React/RCTFont.h>
@implementation RCTHelpers
+ (NSMutableDictionary *)textAttributesFromDictionary:(NSDictionary *)dictionary withPrefix:(NSString *)prefix baseFont:(UIFont *)baseFont
{
NSMutableDictionary *textAttributes = [NSMutableDictionary new];
NSString *colorKey = @"color";
NSString *familyKey = @"fontFamily";
NSString *weightKey = @"fontWeight";
NSString *sizeKey = @"fontSize";
NSString *styleKey = @"fontStyle";
NSString *shadowColourKey = @"shadowColor";
NSString *shadowOffsetKey = @"shadowOffset";
NSString *shadowBlurRadiusKey = @"shadowBlurRadius";
NSString *showShadowKey = @"showShadow";
if (prefix) {
colorKey = [colorKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[colorKey substringToIndex:1].capitalizedString];
colorKey = [NSString stringWithFormat:@"%@%@", prefix, colorKey];
familyKey = [familyKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[familyKey substringToIndex:1].capitalizedString];
familyKey = [NSString stringWithFormat:@"%@%@", prefix, familyKey];
weightKey = [weightKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[weightKey substringToIndex:1].capitalizedString];
weightKey = [NSString stringWithFormat:@"%@%@", prefix, weightKey];
sizeKey = [sizeKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[sizeKey substringToIndex:1].capitalizedString];
sizeKey = [NSString stringWithFormat:@"%@%@", prefix, sizeKey];
styleKey = [styleKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[styleKey substringToIndex:1].capitalizedString];
styleKey = [NSString stringWithFormat:@"%@%@", prefix, styleKey];
shadowColourKey = [shadowColourKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[shadowColourKey substringToIndex:1].capitalizedString];
shadowColourKey = [NSString stringWithFormat:@"%@%@", prefix, shadowColourKey];
shadowOffsetKey = [shadowOffsetKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[shadowOffsetKey substringToIndex:1].capitalizedString];
shadowOffsetKey = [NSString stringWithFormat:@"%@%@", prefix, shadowOffsetKey];
shadowBlurRadiusKey = [shadowBlurRadiusKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[shadowBlurRadiusKey substringToIndex:1].capitalizedString];
shadowBlurRadiusKey = [NSString stringWithFormat:@"%@%@", prefix, shadowBlurRadiusKey];
showShadowKey = [showShadowKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[showShadowKey substringToIndex:1].capitalizedString];
showShadowKey = [NSString stringWithFormat:@"%@%@", prefix, showShadowKey];
}
NSShadow *shadow;
NSNumber *shadowColor = dictionary[shadowColourKey];
if (shadowColor && [shadowColor isKindOfClass:[NSNumber class]]) {
if (!shadow) {
shadow = [NSShadow new];
}
shadow.shadowColor = [RCTConvert UIColor:shadowColor];
}
NSDictionary *shadowOffsetDict = dictionary[shadowOffsetKey];
if (shadowOffsetDict && [shadowOffsetDict isKindOfClass:[NSDictionary class]]) {
CGSize shadowOffset = [RCTConvert CGSize:shadowOffsetDict];
if (!shadow) {
shadow = [NSShadow new];
}
shadow.shadowOffset = shadowOffset;
}
NSNumber *shadowRadius = dictionary[shadowBlurRadiusKey];
if (shadowRadius) {
CGFloat radius = [RCTConvert CGFloat:shadowRadius];
if (!shadow) {
shadow = [NSShadow new];
}
shadow.shadowBlurRadius = radius;
}
NSNumber *showShadow = dictionary[showShadowKey];
if (showShadow) {
BOOL show = [RCTConvert BOOL:showShadow];
if (!show) {
shadow = nil;
}
}
if (shadow) {
[textAttributes setObject:shadow forKey:NSShadowAttributeName];
}
NSNumber *textColor = dictionary[colorKey];
if (textColor && [textColor isKindOfClass:[NSNumber class]])
{
UIColor *color = [RCTConvert UIColor:textColor];
[textAttributes setObject:color forKey:NSForegroundColorAttributeName];
}
NSString *fontFamily = dictionary[familyKey];
if (![fontFamily isKindOfClass:[NSString class]]) {
fontFamily = nil;
}
NSString *fontWeight = dictionary[weightKey];
if (![fontWeight isKindOfClass:[NSString class]]) {
fontWeight = nil;
}
NSNumber *fontSize = dictionary[sizeKey];
if (![fontSize isKindOfClass:[NSNumber class]]) {
fontSize = nil;
}
NSString *fontStyle = dictionary[styleKey];
if (![fontStyle isKindOfClass:[NSString class]]) {
fontStyle = nil;
}
UIFont *font = [RCTFont updateFont:baseFont withFamily:fontFamily size:fontSize weight:fontWeight style:fontStyle variant:nil scaleMultiplier:1];
if (font && (fontStyle || fontWeight || fontSize || fontFamily)) {
[textAttributes setObject:font forKey:NSFontAttributeName];
}
return textAttributes;
}
+ (NSMutableDictionary *)textAttributesFromDictionary:(NSDictionary *)dictionary withPrefix:(NSString *)prefix
{
return [self textAttributesFromDictionary:dictionary withPrefix:prefix baseFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]];
}
+ (NSString *)getTimestampString {
long long milliseconds = (long long)([[NSDate date] timeIntervalSince1970] * 1000.0);
return [NSString stringWithFormat:@"%lld", milliseconds];
}
+ (NSArray *)getAllSubviewsForView:(UIView *)view {
NSMutableArray *allSubviews = [NSMutableArray new];
for (UIView *subview in view.subviews)
{
[allSubviews addObject:subview];
[allSubviews addObjectsFromArray:[self getAllSubviewsForView:subview]];
}
return allSubviews;
}
/*
The YellowBox is added to each RCTRootView. Regardless if there are warnings or not, if there's a warning anywhere in the app - it is added
Since it is always appears on the top, it blocks interactions with other components.
It is most noticeable in RCCLightBox and RCCNotification where button (for example) are not clickable if placed at the bottom part of the view
*/
+ (BOOL)removeYellowBox:(RCTRootView *)reactRootView {
#ifndef DEBUG
return YES;
#endif
BOOL removed = NO;
NSArray* subviews = [self getAllSubviewsForView:reactRootView];
for (UIView *view in subviews)
{
if ([view isKindOfClass:[RCTView class]])
{
CGFloat r, g, b, a;
[view.backgroundColor getRed:&r green:&g blue:&b alpha:&a];
//identify the yellow view by its hard-coded color and height
if((lrint(r * 255) == 250) && (lrint(g * 255) == 186) && (lrint(b * 255) == 48) && (lrint(a * 100) == 95) && (view.frame.size.height == 46))
{
UIView *yelloboxParentView = view;
while (view.superview != nil)
{
yelloboxParentView = yelloboxParentView.superview;
if ([yelloboxParentView isKindOfClass:[RCTScrollView class]])
{
yelloboxParentView = yelloboxParentView.superview;
break;
}
}
[yelloboxParentView removeFromSuperview];
removed = YES;
break;
}
}
if (removed)
{
break;
}
}
return removed;
}
@end