react-native-navigation的迁移库

RNNNavigationButtons.m 6.0KB

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