react-native-navigation的迁移库

RNNBottomTabOptions.m 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #import "RNNBottomTabOptions.h"
  2. #import "UIImage+tint.h"
  3. @implementation RNNBottomTabOptions
  4. -(instancetype)initWithDict:(NSDictionary *)tabItemDict {
  5. self = [super init];
  6. [self mergeWith:tabItemDict];
  7. self.tag = [tabItemDict[@"tag"] integerValue];
  8. return self;
  9. }
  10. - (void)applyOn:(UIViewController *)viewController {
  11. UIViewController* topViewController = [self getTabControllerFirstChild:viewController];
  12. if (self.text || self.icon || self.selectedIcon) {
  13. UITabBarItem* tabItem = topViewController.tabBarItem;
  14. tabItem.selectedImage = [self getSelectedIconImage];
  15. tabItem.image = [self getIconImage];
  16. tabItem.title = self.text;
  17. tabItem.tag = self.tag;
  18. tabItem.accessibilityIdentifier = self.testID;
  19. if (self.iconInsets && ![self.iconInsets isKindOfClass:[NSNull class]]) {
  20. id topInset = self.iconInsets[@"top"];
  21. id leftInset = self.iconInsets[@"left"];
  22. id bottomInset = self.iconInsets[@"bottom"];
  23. id rightInset = self.iconInsets[@"right"];
  24. CGFloat top = topInset != (id)[NSNull null] ? [RCTConvert CGFloat:topInset] : 0;
  25. CGFloat left = topInset != (id)[NSNull null] ? [RCTConvert CGFloat:leftInset] : 0;
  26. CGFloat bottom = topInset != (id)[NSNull null] ? [RCTConvert CGFloat:bottomInset] : 0;
  27. CGFloat right = topInset != (id)[NSNull null] ? [RCTConvert CGFloat:rightInset] : 0;
  28. tabItem.imageInsets = UIEdgeInsetsMake(top, left, bottom, right);
  29. }
  30. [self appendTitleAttributes:tabItem];
  31. [topViewController setTabBarItem:tabItem];
  32. }
  33. if (self.badge) {
  34. NSString *badge = nil;
  35. if (self.badge != nil && ![self.badge isEqual:[NSNull null]]) {
  36. badge = [RCTConvert NSString:self.badge];
  37. }
  38. UITabBarItem *tabBarItem = topViewController.tabBarItem;
  39. tabBarItem.badgeValue = badge;
  40. if (self.badgeColor) {
  41. tabBarItem.badgeColor = [RCTConvert UIColor:self.badgeColor];
  42. }
  43. if ([self.badge isEqual:[NSNull null]] || [self.badge isEqualToString:@""]) {
  44. tabBarItem.badgeValue = nil;
  45. }
  46. }
  47. if (self.visible) {
  48. [topViewController.tabBarController setSelectedIndex:[viewController.tabBarController.viewControllers indexOfObject:viewController]];
  49. }
  50. [self resetOptions];
  51. }
  52. - (UIImage *)getIconImage {
  53. return [self getIconImageWithTint:self.iconColor];
  54. }
  55. - (UIImage *)getSelectedIconImage {
  56. if (self.selectedIcon) {
  57. if (self.selectedIconColor) {
  58. return [[[RCTConvert UIImage:self.selectedIcon] withTintColor:[RCTConvert UIColor:self.selectedIconColor]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  59. } else {
  60. return [[RCTConvert UIImage:self.selectedIcon] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  61. }
  62. } else {
  63. return [self getIconImageWithTint:self.selectedIconColor];
  64. }
  65. return nil;
  66. }
  67. - (UIImage *)getIconImageWithTint:(NSDictionary *)tintColor {
  68. if (self.icon) {
  69. if (tintColor) {
  70. return [[[RCTConvert UIImage:self.icon] withTintColor:[RCTConvert UIColor:tintColor]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  71. } else {
  72. return [[RCTConvert UIImage:self.icon] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  73. }
  74. }
  75. return nil;
  76. }
  77. - (void)appendTitleAttributes:(UITabBarItem *)tabItem {
  78. NSMutableDictionary* selectedAttributes = [NSMutableDictionary dictionaryWithDictionary:[tabItem titleTextAttributesForState:UIControlStateNormal]];
  79. if (self.selectedTextColor) {
  80. selectedAttributes[NSForegroundColorAttributeName] = [RCTConvert UIColor:self.selectedTextColor];
  81. } else {
  82. selectedAttributes[NSForegroundColorAttributeName] = [UIColor blackColor];
  83. }
  84. selectedAttributes[NSFontAttributeName] = [self tabBarTextFont];
  85. [tabItem setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
  86. NSMutableDictionary* normalAttributes = [NSMutableDictionary dictionaryWithDictionary:[tabItem titleTextAttributesForState:UIControlStateNormal]];
  87. if (self.textColor) {
  88. normalAttributes[NSForegroundColorAttributeName] = [RCTConvert UIColor:self.textColor];
  89. } else {
  90. normalAttributes[NSForegroundColorAttributeName] = [UIColor blackColor];
  91. }
  92. normalAttributes[NSFontAttributeName] = [self tabBarTextFont];
  93. [tabItem setTitleTextAttributes:normalAttributes forState:UIControlStateNormal];
  94. }
  95. -(UIFont *)tabBarTextFont {
  96. if (self.fontFamily) {
  97. return [UIFont fontWithName:self.fontFamily size:self.tabBarTextFontSizeValue];
  98. }
  99. else if (self.fontSize) {
  100. return [UIFont systemFontOfSize:self.tabBarTextFontSizeValue];
  101. }
  102. else {
  103. return nil;
  104. }
  105. }
  106. -(CGFloat)tabBarTextFontSizeValue {
  107. return self.fontSize ? [self.fontSize floatValue] : 10;
  108. }
  109. - (UIViewController *)getTabControllerFirstChild:(UIViewController *)viewController {
  110. while (viewController != nil) {
  111. if ([viewController.parentViewController isKindOfClass:[UITabBarController class]] || !viewController.parentViewController) {
  112. return viewController;
  113. }
  114. viewController = viewController.parentViewController;
  115. }
  116. return nil;
  117. }
  118. -(void)resetOptions {
  119. self.text = nil;
  120. self.badge = nil;
  121. self.visible = nil;
  122. self.icon = nil;
  123. self.testID = nil;
  124. self.iconInsets = nil;
  125. self.selectedIcon = nil;
  126. }
  127. @end