react-native-navigation的迁移库

RNNNavigationButtons.m 5.5KB

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