react-native-navigation的迁移库

RCTHelpers.m 7.6KB

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