react-native-navigation的迁移库

RNNBottomTabOptions.m 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. }
  46. if (self.visible) {
  47. [viewController.tabBarController setSelectedIndex:[viewController.tabBarController.viewControllers indexOfObject:viewController]];
  48. }
  49. [self resetOptions];
  50. }
  51. - (UIImage *)getIconImage {
  52. return [self getIconImageWithTint:self.iconColor];
  53. }
  54. - (UIImage *)getSelectedIconImage {
  55. if (self.selectedIcon) {
  56. if (self.selectedIconColor) {
  57. return [[[RCTConvert UIImage:self.selectedIcon] withTintColor:[RCTConvert UIColor:self.selectedIconColor]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  58. } else {
  59. return [[RCTConvert UIImage:self.selectedIcon] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  60. }
  61. } else {
  62. return [self getIconImageWithTint:self.selectedIconColor];
  63. }
  64. return nil;
  65. }
  66. - (UIImage *)getIconImageWithTint:(NSDictionary *)tintColor {
  67. if (self.icon) {
  68. if (tintColor) {
  69. return [[[RCTConvert UIImage:self.icon] withTintColor:[RCTConvert UIColor:tintColor]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  70. } else {
  71. return [[RCTConvert UIImage:self.icon] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  72. }
  73. }
  74. return nil;
  75. }
  76. - (void)appendTitleAttributes:(UITabBarItem *)tabItem {
  77. NSMutableDictionary* selectedAttributes = [NSMutableDictionary dictionaryWithDictionary:[tabItem titleTextAttributesForState:UIControlStateNormal]];
  78. if (self.selectedTextColor) {
  79. selectedAttributes[NSForegroundColorAttributeName] = [RCTConvert UIColor:self.selectedTextColor];
  80. } else {
  81. selectedAttributes[NSForegroundColorAttributeName] = [UIColor blackColor];
  82. }
  83. selectedAttributes[NSFontAttributeName] = [self tabBarTextFont];
  84. [tabItem setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
  85. NSMutableDictionary* normalAttributes = [NSMutableDictionary dictionaryWithDictionary:[tabItem titleTextAttributesForState:UIControlStateNormal]];
  86. if (self.textColor) {
  87. normalAttributes[NSForegroundColorAttributeName] = [RCTConvert UIColor:self.textColor];
  88. } else {
  89. normalAttributes[NSForegroundColorAttributeName] = [UIColor blackColor];
  90. }
  91. normalAttributes[NSFontAttributeName] = [self tabBarTextFont];
  92. [tabItem setTitleTextAttributes:normalAttributes forState:UIControlStateNormal];
  93. }
  94. -(UIFont *)tabBarTextFont {
  95. if (self.fontFamily) {
  96. return [UIFont fontWithName:self.fontFamily size:self.tabBarTextFontSizeValue];
  97. }
  98. else if (self.fontSize) {
  99. return [UIFont systemFontOfSize:self.tabBarTextFontSizeValue];
  100. }
  101. else {
  102. return nil;
  103. }
  104. }
  105. -(CGFloat)tabBarTextFontSizeValue {
  106. return self.fontSize ? [self.fontSize floatValue] : 10;
  107. }
  108. -(void)resetOptions {
  109. self.text = nil;
  110. self.badge = nil;
  111. self.visible = nil;
  112. self.icon = nil;
  113. self.testID = nil;
  114. self.iconInsets = nil;
  115. self.selectedIcon = nil;
  116. }
  117. @end