react-native-navigation的迁移库

RNNNavigationButtons.m 5.7KB

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