react-native-navigation的迁移库

RNNTabBarItemCreator.m 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #import "RNNTabBarItemCreator.h"
  2. #import "UIImage+tint.h"
  3. #import "RNNFontAttributesCreator.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:nil];
  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] fontWeight:[bottomTabOptions.fontWeight 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 fontWeight:(NSString *)fontWeight {
  51. NSDictionary* selectedAttributes = [RNNFontAttributesCreator createFromDictionary:[tabItem titleTextAttributesForState:UIControlStateSelected] fontFamily:fontFamily fontSize:fontSize defaultFontSize:@(10) fontWeight:fontWeight color:selectedTextColor defaultColor:[UIColor blackColor]];
  52. [tabItem setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
  53. NSDictionary* normalAttributes = [RNNFontAttributesCreator createFromDictionary:[tabItem titleTextAttributesForState:UIControlStateNormal] fontFamily:fontFamily fontSize:fontSize defaultFontSize:@(10) fontWeight:fontWeight color:textColor defaultColor:[UIColor blackColor]];
  54. [tabItem setTitleTextAttributes:normalAttributes forState:UIControlStateNormal];
  55. }
  56. @end