react-native-navigation的迁移库

RNNBottomTabOptions.m 4.4KB

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