react-native-navigation的迁移库

RCTHelpers.m 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #import "RCTHelpers.h"
  2. #import <React/RCTView.h>
  3. #import <React/RCTScrollView.h>
  4. #import <React/RCTFont.h>
  5. @implementation RCTHelpers
  6. + (NSMutableDictionary *)textAttributesFromDictionary:(NSDictionary *)dictionary withPrefix:(NSString *)prefix baseFont:(UIFont *)baseFont
  7. {
  8. NSMutableDictionary *textAttributes = [NSMutableDictionary new];
  9. NSString *colorKey = @"color";
  10. NSString *familyKey = @"fontFamily";
  11. NSString *weightKey = @"fontWeight";
  12. NSString *sizeKey = @"fontSize";
  13. NSString *styleKey = @"fontStyle";
  14. NSString *shadowColourKey = @"shadowColor";
  15. NSString *shadowOffsetKey = @"shadowOffset";
  16. NSString *shadowBlurRadiusKey = @"shadowBlurRadius";
  17. NSString *showShadowKey = @"showShadow";
  18. if (prefix) {
  19. colorKey = [colorKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[colorKey substringToIndex:1].capitalizedString];
  20. colorKey = [NSString stringWithFormat:@"%@%@", prefix, colorKey];
  21. familyKey = [familyKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[familyKey substringToIndex:1].capitalizedString];
  22. familyKey = [NSString stringWithFormat:@"%@%@", prefix, familyKey];
  23. weightKey = [weightKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[weightKey substringToIndex:1].capitalizedString];
  24. weightKey = [NSString stringWithFormat:@"%@%@", prefix, weightKey];
  25. sizeKey = [sizeKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[sizeKey substringToIndex:1].capitalizedString];
  26. sizeKey = [NSString stringWithFormat:@"%@%@", prefix, sizeKey];
  27. styleKey = [styleKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[styleKey substringToIndex:1].capitalizedString];
  28. styleKey = [NSString stringWithFormat:@"%@%@", prefix, styleKey];
  29. shadowColourKey = [shadowColourKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[shadowColourKey substringToIndex:1].capitalizedString];
  30. shadowColourKey = [NSString stringWithFormat:@"%@%@", prefix, shadowColourKey];
  31. shadowOffsetKey = [shadowOffsetKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[shadowOffsetKey substringToIndex:1].capitalizedString];
  32. shadowOffsetKey = [NSString stringWithFormat:@"%@%@", prefix, shadowOffsetKey];
  33. shadowBlurRadiusKey = [shadowBlurRadiusKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[shadowBlurRadiusKey substringToIndex:1].capitalizedString];
  34. shadowBlurRadiusKey = [NSString stringWithFormat:@"%@%@", prefix, shadowBlurRadiusKey];
  35. showShadowKey = [showShadowKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[showShadowKey substringToIndex:1].capitalizedString];
  36. showShadowKey = [NSString stringWithFormat:@"%@%@", prefix, showShadowKey];
  37. }
  38. NSShadow *shadow;
  39. NSNumber *shadowColor = dictionary[shadowColourKey];
  40. if (shadowColor && [shadowColor isKindOfClass:[NSNumber class]]) {
  41. if (!shadow) {
  42. shadow = [NSShadow new];
  43. }
  44. shadow.shadowColor = [RCTConvert UIColor:shadowColor];
  45. }
  46. NSDictionary *shadowOffsetDict = dictionary[shadowOffsetKey];
  47. if (shadowOffsetDict && [shadowOffsetDict isKindOfClass:[NSDictionary class]]) {
  48. CGSize shadowOffset = [RCTConvert CGSize:shadowOffsetDict];
  49. if (!shadow) {
  50. shadow = [NSShadow new];
  51. }
  52. shadow.shadowOffset = shadowOffset;
  53. }
  54. NSNumber *shadowRadius = dictionary[shadowBlurRadiusKey];
  55. if (shadowRadius) {
  56. CGFloat radius = [RCTConvert CGFloat:shadowRadius];
  57. if (!shadow) {
  58. shadow = [NSShadow new];
  59. }
  60. shadow.shadowBlurRadius = radius;
  61. }
  62. NSNumber *showShadow = dictionary[showShadowKey];
  63. if (showShadow) {
  64. BOOL show = [RCTConvert BOOL:showShadow];
  65. if (!show) {
  66. shadow = nil;
  67. }
  68. }
  69. if (shadow) {
  70. [textAttributes setObject:shadow forKey:NSShadowAttributeName];
  71. }
  72. NSNumber *textColor = dictionary[colorKey];
  73. if (textColor && [textColor isKindOfClass:[NSNumber class]])
  74. {
  75. UIColor *color = [RCTConvert UIColor:textColor];
  76. [textAttributes setObject:color forKey:NSForegroundColorAttributeName];
  77. }
  78. NSString *fontFamily = dictionary[familyKey];
  79. if (![fontFamily isKindOfClass:[NSString class]]) {
  80. fontFamily = nil;
  81. }
  82. NSString *fontWeight = dictionary[weightKey];
  83. if (![fontWeight isKindOfClass:[NSString class]]) {
  84. fontWeight = nil;
  85. }
  86. NSNumber *fontSize = dictionary[sizeKey];
  87. if (![fontSize isKindOfClass:[NSNumber class]]) {
  88. fontSize = nil;
  89. }
  90. NSString *fontStyle = dictionary[styleKey];
  91. if (![fontStyle isKindOfClass:[NSString class]]) {
  92. fontStyle = nil;
  93. }
  94. UIFont *font = [RCTFont updateFont:baseFont withFamily:fontFamily size:fontSize weight:fontWeight style:fontStyle variant:nil scaleMultiplier:1];
  95. if (font && (fontStyle || fontWeight || fontSize || fontFamily)) {
  96. [textAttributes setObject:font forKey:NSFontAttributeName];
  97. }
  98. return textAttributes;
  99. }
  100. + (NSMutableDictionary *)textAttributesFromDictionary:(NSDictionary *)dictionary withPrefix:(NSString *)prefix
  101. {
  102. return [self textAttributesFromDictionary:dictionary withPrefix:prefix baseFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]];
  103. }
  104. + (NSString *)getTimestampString {
  105. long long milliseconds = (long long)([[NSDate date] timeIntervalSince1970] * 1000.0);
  106. return [NSString stringWithFormat:@"%lld", milliseconds];
  107. }
  108. + (NSArray *)getAllSubviewsForView:(UIView *)view {
  109. NSMutableArray *allSubviews = [NSMutableArray new];
  110. for (UIView *subview in view.subviews)
  111. {
  112. [allSubviews addObject:subview];
  113. [allSubviews addObjectsFromArray:[self getAllSubviewsForView:subview]];
  114. }
  115. return allSubviews;
  116. }
  117. /*
  118. 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
  119. Since it is always appears on the top, it blocks interactions with other components.
  120. It is most noticeable in RCCLightBox and RCCNotification where button (for example) are not clickable if placed at the bottom part of the view
  121. */
  122. + (BOOL)removeYellowBox:(RCTRootView *)reactRootView {
  123. #ifndef DEBUG
  124. return YES;
  125. #endif
  126. BOOL removed = NO;
  127. NSArray* subviews = [self getAllSubviewsForView:reactRootView];
  128. for (UIView *view in subviews)
  129. {
  130. if ([view isKindOfClass:[RCTView class]])
  131. {
  132. CGFloat r, g, b, a;
  133. [view.backgroundColor getRed:&r green:&g blue:&b alpha:&a];
  134. //identify the yellow view by its hard-coded color and height
  135. if((lrint(r * 255) == 250) && (lrint(g * 255) == 186) && (lrint(b * 255) == 48) && (lrint(a * 100) == 95) && (view.frame.size.height == 48))
  136. {
  137. UIView *yelloboxParentView = view;
  138. while (view.superview != nil)
  139. {
  140. yelloboxParentView = yelloboxParentView.superview;
  141. if ([yelloboxParentView isKindOfClass:[RCTScrollView class]])
  142. {
  143. yelloboxParentView = yelloboxParentView.superview;
  144. break;
  145. }
  146. }
  147. [yelloboxParentView removeFromSuperview];
  148. removed = YES;
  149. break;
  150. }
  151. }
  152. if (removed)
  153. {
  154. break;
  155. }
  156. }
  157. return removed;
  158. }
  159. @end