react-native-navigation的迁移库

RNNBottomTabOptions.m 4.6KB

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