react-native-navigation的迁移库

RNNNavigationButtons.m 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. if (!buttonId) {
  58. @throw [NSException exceptionWithName:@"NSInvalidArgumentException" reason:[@"button id is not specified " stringByAppendingString:title] userInfo:nil];
  59. }
  60. UIImage* defaultIcon = [defaultStyle.icon getWithDefaultValue:nil];
  61. UIImage* iconImage = [self getValue:dictionary[@"icon"] withDefault:defaultIcon];
  62. if (![iconImage isKindOfClass:[UIImage class]]) {
  63. iconImage = [RCTConvert UIImage:iconImage];
  64. }
  65. RNNUIBarButtonItem *barButtonItem;
  66. if (component) {
  67. RCTRootView *view = (RCTRootView*)[self.creator createCustomReactView:component[@"name"] rootViewId:component[@"componentId"]];
  68. barButtonItem = [[RNNUIBarButtonItem alloc] init:buttonId withCustomView:view];
  69. } else if (iconImage) {
  70. barButtonItem = [[RNNUIBarButtonItem alloc] init:buttonId withIcon:iconImage];
  71. } else if (title) {
  72. barButtonItem = [[RNNUIBarButtonItem alloc] init:buttonId withTitle:title];
  73. NSMutableDictionary *buttonTextAttributes = [RCTHelpers textAttributesFromDictionary:dictionary withPrefix:@"button"];
  74. if (buttonTextAttributes.allKeys.count > 0) {
  75. [barButtonItem setTitleTextAttributes:buttonTextAttributes forState:UIControlStateNormal];
  76. }
  77. } else {
  78. return nil;
  79. }
  80. barButtonItem.target = self.viewController;
  81. barButtonItem.action = @selector(onButtonPress:);
  82. NSNumber *enabled = [self getValue:dictionary[@"enabled"] withDefault:defaultStyle.enabled.getValue];
  83. BOOL enabledBool = enabled ? [enabled boolValue] : YES;
  84. [barButtonItem setEnabled:enabledBool];
  85. NSMutableDictionary* textAttributes = [[NSMutableDictionary alloc] init];
  86. NSMutableDictionary* disabledTextAttributes = [[NSMutableDictionary alloc] init];
  87. UIColor* color = [self color:[RCTConvert UIColor:dictionary[@"color"]] defaultColor:[defaultStyle.color getWithDefaultValue:nil]];
  88. UIColor* disabledColor = [self color:[RCTConvert UIColor:dictionary[@"disabledColor"]] defaultColor:[defaultStyle.disabledColor getWithDefaultValue:nil]];
  89. if (!enabledBool && disabledColor) {
  90. color = disabledColor;
  91. [disabledTextAttributes setObject:disabledColor forKey:NSForegroundColorAttributeName];
  92. }
  93. if (color) {
  94. [textAttributes setObject:color forKey:NSForegroundColorAttributeName];
  95. [barButtonItem setImage:[[iconImage withTintColor:color] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
  96. }
  97. NSNumber* fontSize = [self fontSize:dictionary[@"fontSize"] defaultFontSize:[defaultStyle.fontSize getWithDefaultValue:nil]];
  98. NSString* fontFamily = [self fontFamily:dictionary[@"fontFamily"] defaultFontFamily:[defaultStyle.fontFamily getWithDefaultValue:nil]];
  99. UIFont *font = nil;
  100. if (fontFamily) {
  101. font = [UIFont fontWithName:fontFamily size:[fontSize floatValue]];
  102. } else {
  103. font = [UIFont systemFontOfSize:[fontSize floatValue]];
  104. }
  105. [textAttributes setObject:font forKey:NSFontAttributeName];
  106. [disabledTextAttributes setObject:font forKey:NSFontAttributeName];
  107. [barButtonItem setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
  108. [barButtonItem setTitleTextAttributes:textAttributes forState:UIControlStateHighlighted];
  109. [barButtonItem setTitleTextAttributes:disabledTextAttributes forState:UIControlStateDisabled];
  110. NSString *testID = dictionary[@"testID"];
  111. if (testID)
  112. {
  113. barButtonItem.accessibilityIdentifier = testID;
  114. }
  115. return barButtonItem;
  116. }
  117. - (UIColor *)color:(UIColor *)color defaultColor:(UIColor *)defaultColor {
  118. if (color) {
  119. return color;
  120. } else if (defaultColor) {
  121. return defaultColor;
  122. }
  123. return nil;
  124. }
  125. - (NSNumber *)fontSize:(NSNumber *)fontSize defaultFontSize:(NSNumber *)defaultFontSize {
  126. if (fontSize) {
  127. return fontSize;
  128. } else if (defaultFontSize) {
  129. return defaultFontSize;
  130. }
  131. return @(17);
  132. }
  133. - (NSString *)fontFamily:(NSString *)fontFamily defaultFontFamily:(NSString *)defaultFontFamily {
  134. if (fontFamily) {
  135. return fontFamily;
  136. } else {
  137. return defaultFontFamily;
  138. }
  139. }
  140. - (id)getValue:(id)value withDefault:(id)defaultValue {
  141. return value ? value : defaultValue;
  142. }
  143. @end