react-native-navigation的迁移库

RNNTabBarItemCreator.m 4.2KB

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