react-native-navigation的迁移库

RNNNavigationButtons.m 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #import "RNNNavigationButtons.h"
  2. #import "RNNUIBarButtonItem.h"
  3. #import <React/RCTConvert.h>
  4. #import "RCTHelpers.h"
  5. #import "UIImage+tint.h"
  6. #import "RNNRootViewController.h"
  7. @interface RNNNavigationButtons()
  8. @property (weak, nonatomic) UIViewController* viewController;
  9. @property (strong, nonatomic) RNNButtonOptions* defaultLeftButtonStyle;
  10. @property (strong, nonatomic) RNNButtonOptions* defaultRightButtonStyle;
  11. @property (nonatomic) id<RNNRootViewCreator> creator;
  12. @end
  13. @implementation RNNNavigationButtons
  14. -(instancetype)initWithViewController:(UIViewController*)viewController rootViewCreator:(id<RNNRootViewCreator>)creator {
  15. self = [super init];
  16. self.viewController = viewController;
  17. self.creator = creator;
  18. return self;
  19. }
  20. - (void)applyLeftButtons:(NSArray *)leftButtons rightButtons:(NSArray *)rightButtons defaultLeftButtonStyle:(RNNButtonOptions *)defaultLeftButtonStyle defaultRightButtonStyle:(RNNButtonOptions *)defaultRightButtonStyle {
  21. _defaultLeftButtonStyle = defaultLeftButtonStyle;
  22. _defaultRightButtonStyle = defaultRightButtonStyle;
  23. if (leftButtons) {
  24. [self setButtons:leftButtons side:@"left" animated:NO defaultStyle:_defaultLeftButtonStyle];
  25. }
  26. if (rightButtons) {
  27. [self setButtons:rightButtons side:@"right" animated:NO defaultStyle:_defaultRightButtonStyle];
  28. }
  29. }
  30. -(void)setButtons:(NSArray*)buttons side:(NSString*)side animated:(BOOL)animated defaultStyle:(RNNButtonOptions *)defaultStyle {
  31. NSMutableArray *barButtonItems = [NSMutableArray new];
  32. NSArray* resolvedButtons = [self resolveButtons:buttons];
  33. for (NSDictionary *button in resolvedButtons) {
  34. RNNUIBarButtonItem* barButtonItem = [self buildButton:button defaultStyle:defaultStyle];
  35. if(barButtonItem) {
  36. [barButtonItems addObject:barButtonItem];
  37. }
  38. }
  39. if ([side isEqualToString:@"left"]) {
  40. [self.viewController.navigationItem setLeftBarButtonItems:barButtonItems animated:animated];
  41. }
  42. if ([side isEqualToString:@"right"]) {
  43. [self.viewController.navigationItem setRightBarButtonItems:barButtonItems animated:animated];
  44. }
  45. }
  46. - (NSArray *)resolveButtons:(id)buttons {
  47. if ([buttons isKindOfClass:[NSArray class]]) {
  48. return buttons;
  49. } else {
  50. return @[buttons];
  51. }
  52. }
  53. -(RNNUIBarButtonItem*)buildButton: (NSDictionary*)dictionary defaultStyle:(RNNButtonOptions *)defaultStyle {
  54. NSString* buttonId = dictionary[@"id"];
  55. NSString* title = [self getValue:dictionary[@"text"] withDefault:[defaultStyle.text getWithDefaultValue:nil]];
  56. NSDictionary* component = dictionary[@"component"];
  57. NSString* systemItemName = dictionary[@"systemItem"];
  58. if (!buttonId) {
  59. @throw [NSException exceptionWithName:@"NSInvalidArgumentException" reason:[@"button id is not specified " stringByAppendingString:title] userInfo:nil];
  60. }
  61. UIImage* defaultIcon = [defaultStyle.icon getWithDefaultValue:nil];
  62. UIImage* iconImage = [self getValue:dictionary[@"icon"] withDefault:defaultIcon];
  63. if (![iconImage isKindOfClass:[UIImage class]]) {
  64. iconImage = [RCTConvert UIImage:iconImage];
  65. }
  66. RNNUIBarButtonItem *barButtonItem;
  67. if (component) {
  68. RCTRootView *view = (RCTRootView*)[self.creator createCustomReactView:component[@"name"] rootViewId:component[@"componentId"]];
  69. barButtonItem = [[RNNUIBarButtonItem alloc] init:buttonId withCustomView:view];
  70. } else if (iconImage) {
  71. barButtonItem = [[RNNUIBarButtonItem alloc] init:buttonId withIcon:iconImage];
  72. } else if (title) {
  73. barButtonItem = [[RNNUIBarButtonItem alloc] init:buttonId withTitle:title];
  74. NSMutableDictionary *buttonTextAttributes = [RCTHelpers textAttributesFromDictionary:dictionary withPrefix:@"button"];
  75. if (buttonTextAttributes.allKeys.count > 0) {
  76. [barButtonItem setTitleTextAttributes:buttonTextAttributes forState:UIControlStateNormal];
  77. }
  78. } else if (systemItemName) {
  79. barButtonItem = [[RNNUIBarButtonItem alloc] init:buttonId withSystemItem:systemItemName];
  80. } else {
  81. return nil;
  82. }
  83. barButtonItem.target = self.viewController;
  84. barButtonItem.action = @selector(onButtonPress:);
  85. NSNumber *enabled = [self getValue:dictionary[@"enabled"] withDefault:defaultStyle.enabled.getValue];
  86. BOOL enabledBool = enabled ? [enabled boolValue] : YES;
  87. [barButtonItem setEnabled:enabledBool];
  88. NSMutableDictionary* textAttributes = [[NSMutableDictionary alloc] init];
  89. NSMutableDictionary* disabledTextAttributes = [[NSMutableDictionary alloc] init];
  90. UIColor* color = [self color:[RCTConvert UIColor:dictionary[@"color"]] defaultColor:[defaultStyle.color getWithDefaultValue:nil]];
  91. UIColor* disabledColor = [self color:[RCTConvert UIColor:dictionary[@"disabledColor"]] defaultColor:[defaultStyle.disabledColor getWithDefaultValue:nil]];
  92. if (!enabledBool && disabledColor) {
  93. color = disabledColor;
  94. [disabledTextAttributes setObject:disabledColor forKey:NSForegroundColorAttributeName];
  95. }
  96. if (color) {
  97. [textAttributes setObject:color forKey:NSForegroundColorAttributeName];
  98. [barButtonItem setImage:[[iconImage withTintColor:color] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
  99. }
  100. NSNumber* fontSize = [self fontSize:dictionary[@"fontSize"] defaultFontSize:[defaultStyle.fontSize getWithDefaultValue:nil]];
  101. NSString* fontFamily = [self fontFamily:dictionary[@"fontFamily"] defaultFontFamily:[defaultStyle.fontFamily getWithDefaultValue:nil]];
  102. UIFont *font = nil;
  103. if (fontFamily) {
  104. font = [UIFont fontWithName:fontFamily size:[fontSize floatValue]];
  105. } else {
  106. font = [UIFont systemFontOfSize:[fontSize floatValue]];
  107. }
  108. [textAttributes setObject:font forKey:NSFontAttributeName];
  109. [disabledTextAttributes setObject:font forKey:NSFontAttributeName];
  110. [barButtonItem setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
  111. [barButtonItem setTitleTextAttributes:textAttributes forState:UIControlStateHighlighted];
  112. [barButtonItem setTitleTextAttributes:disabledTextAttributes forState:UIControlStateDisabled];
  113. NSString *testID = dictionary[@"testID"];
  114. if (testID)
  115. {
  116. barButtonItem.accessibilityIdentifier = testID;
  117. }
  118. return barButtonItem;
  119. }
  120. - (UIColor *)color:(UIColor *)color defaultColor:(UIColor *)defaultColor {
  121. if (color) {
  122. return color;
  123. } else if (defaultColor) {
  124. return defaultColor;
  125. }
  126. return nil;
  127. }
  128. - (NSNumber *)fontSize:(NSNumber *)fontSize defaultFontSize:(NSNumber *)defaultFontSize {
  129. if (fontSize) {
  130. return fontSize;
  131. } else if (defaultFontSize) {
  132. return defaultFontSize;
  133. }
  134. return @(17);
  135. }
  136. - (NSString *)fontFamily:(NSString *)fontFamily defaultFontFamily:(NSString *)defaultFontFamily {
  137. if (fontFamily) {
  138. return fontFamily;
  139. } else {
  140. return defaultFontFamily;
  141. }
  142. }
  143. - (id)getValue:(id)value withDefault:(id)defaultValue {
  144. return value ? value : defaultValue;
  145. }
  146. @end