react-native-navigation的迁移库

RNNTabBarItemCreator.m 4.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #import "RNNTabBarItemCreator.h"
  2. #import "UIImage+tint.h"
  3. #import "RNNFontAttributesCreator.h"
  4. @implementation RNNTabBarItemCreator
  5. + (UITabBarItem *)createTabBarItem:(UITabBarItem *)mergeItem {
  6. return [UITabBarItem new];
  7. }
  8. + (UITabBarItem *)createTabBarItem:(RNNBottomTabOptions *)bottomTabOptions mergeItem:(UITabBarItem *)mergeItem {
  9. UITabBarItem* tabItem = [self createTabBarItem:mergeItem];
  10. UIImage* icon = [bottomTabOptions.icon getWithDefaultValue:nil];
  11. UIImage* selectedIcon = [bottomTabOptions.selectedIcon getWithDefaultValue:icon];
  12. UIColor* iconColor = [bottomTabOptions.iconColor getWithDefaultValue:nil];
  13. UIColor* selectedIconColor = [bottomTabOptions.selectedIconColor getWithDefaultValue:iconColor];
  14. tabItem.image = [self getIconImage:icon withTint:iconColor];
  15. tabItem.selectedImage = [self getSelectedIconImage:selectedIcon selectedIconColor:selectedIconColor];
  16. tabItem.title = [bottomTabOptions.text getWithDefaultValue:nil];
  17. tabItem.tag = bottomTabOptions.tag;
  18. tabItem.accessibilityIdentifier = [bottomTabOptions.testID getWithDefaultValue:nil];
  19. NSDictionary* iconInsets = [bottomTabOptions.iconInsets getWithDefaultValue:nil];
  20. if (iconInsets && ![iconInsets isKindOfClass:[NSNull class]]) {
  21. id topInset = iconInsets[@"top"];
  22. id leftInset = iconInsets[@"left"];
  23. id bottomInset = iconInsets[@"bottom"];
  24. id rightInset = iconInsets[@"right"];
  25. CGFloat top = topInset != (id)[NSNull null] ? [RCTConvert CGFloat:topInset] : 0;
  26. CGFloat left = topInset != (id)[NSNull null] ? [RCTConvert CGFloat:leftInset] : 0;
  27. CGFloat bottom = topInset != (id)[NSNull null] ? [RCTConvert CGFloat:bottomInset] : 0;
  28. CGFloat right = topInset != (id)[NSNull null] ? [RCTConvert CGFloat:rightInset] : 0;
  29. tabItem.imageInsets = UIEdgeInsetsMake(top, left, bottom, right);
  30. }
  31. [self appendTitleAttributes:tabItem bottomTabOptions:bottomTabOptions];
  32. return tabItem;
  33. }
  34. + (UIImage *)getSelectedIconImage:(UIImage *)selectedIcon selectedIconColor:(UIColor *)selectedIconColor {
  35. if (selectedIcon) {
  36. if (selectedIconColor) {
  37. return [[selectedIcon withTintColor:selectedIconColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  38. } else {
  39. return [selectedIcon imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  40. }
  41. }
  42. return nil;
  43. }
  44. + (UIImage *)getIconImage:(UIImage *)icon withTint:(UIColor *)tintColor {
  45. if (icon) {
  46. if (tintColor) {
  47. return [[icon withTintColor:tintColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  48. } else {
  49. return [icon imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  50. }
  51. }
  52. return nil;
  53. }
  54. + (void)appendTitleAttributes:(UITabBarItem *)tabItem bottomTabOptions:(RNNBottomTabOptions *)bottomTabOptions {
  55. UIColor* textColor = [bottomTabOptions.textColor getWithDefaultValue:nil];
  56. UIColor* selectedTextColor = [bottomTabOptions.selectedTextColor getWithDefaultValue:nil];
  57. NSString* fontFamily = [bottomTabOptions.fontFamily getWithDefaultValue:nil];
  58. NSNumber* fontSize = [bottomTabOptions.fontSize getWithDefaultValue:nil];
  59. NSString* fontWeight = [bottomTabOptions.fontWeight getWithDefaultValue:nil];
  60. NSDictionary* selectedAttributes = [RNNFontAttributesCreator createFromDictionary:[tabItem titleTextAttributesForState:UIControlStateSelected] fontFamily:fontFamily fontSize:fontSize defaultFontSize:@(10) fontWeight:fontWeight color:selectedTextColor defaultColor:[UIColor blackColor]];
  61. [self setSelectedTitleAttributes:tabItem selectedTitleAttributes:selectedAttributes];
  62. NSDictionary* normalAttributes = [RNNFontAttributesCreator createFromDictionary:[tabItem titleTextAttributesForState:UIControlStateNormal] fontFamily:fontFamily fontSize:fontSize defaultFontSize:@(10) fontWeight:fontWeight color:textColor defaultColor:[UIColor blackColor]];
  63. [self setTitleAttributes:tabItem titleAttributes:normalAttributes];
  64. }
  65. + (void)setTitleAttributes:(UITabBarItem *)tabItem titleAttributes:(NSDictionary *)titleAttributes {
  66. [tabItem setTitleTextAttributes:titleAttributes forState:UIControlStateNormal];
  67. }
  68. + (void)setSelectedTitleAttributes:(UITabBarItem *)tabItem selectedTitleAttributes:(NSDictionary *)selectedTitleAttributes {
  69. [tabItem setTitleTextAttributes:selectedTitleAttributes forState:UIControlStateSelected];
  70. }
  71. @end