react-native-navigation的迁移库

RNNTabBarItemCreator.m 4.2KB

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