Browse Source

[iOS] Different Tabbar item text colors (#1276)

Added different tabbar label colors
Berkay Beyaz 7 years ago
parent
commit
c4f109e553
2 changed files with 21 additions and 9 deletions
  1. 3
    0
      docs/styling-the-tab-bar.md
  2. 18
    9
      ios/RCCTabBarController.m

+ 3
- 0
docs/styling-the-tab-bar.md View File

20
   tabBarBackgroundColor: '#551A8B' // change the background color of the tab bar
20
   tabBarBackgroundColor: '#551A8B' // change the background color of the tab bar
21
   tabBarTranslucent: false // change the translucent of the tab bar to false
21
   tabBarTranslucent: false // change the translucent of the tab bar to false
22
   tabBarTextFontFamily: 'Avenir-Medium' //change the tab font family
22
   tabBarTextFontFamily: 'Avenir-Medium' //change the tab font family
23
+  tabBarLabelColor: '#ffb700', // iOS only. change the color of tab text
24
+  tabBarSelectedLabelColor: 'red', // iOS only. change the color of the selected tab text
25
+  forceTitlesDisplay: true // Android only. If true - Show all bottom tab labels. If false - only the selected tab's label is visible.
23
   tabBarHideShadow: true // iOS only. Remove default tab bar top shadow (hairline)
26
   tabBarHideShadow: true // iOS only. Remove default tab bar top shadow (hairline)
24
   forceTitlesDisplay: true // Android only. If true - Show all bottom tab labels. If false - only the selected tab's label is visible.
27
   forceTitlesDisplay: true // Android only. If true - Show all bottom tab labels. If false - only the selected tab's label is visible.
25
 }
28
 }

+ 18
- 9
ios/RCCTabBarController.m View File

71
   
71
   
72
   UIColor *buttonColor = nil;
72
   UIColor *buttonColor = nil;
73
   UIColor *selectedButtonColor = nil;
73
   UIColor *selectedButtonColor = nil;
74
+  UIColor *labelColor = nil;
75
+  UIColor *selectedLabelColor = nil;
74
   NSDictionary *tabsStyle = props[@"style"];
76
   NSDictionary *tabsStyle = props[@"style"];
75
   if (tabsStyle)
77
   if (tabsStyle)
76
   {
78
   {
82
       buttonColor = color;
84
       buttonColor = color;
83
       selectedButtonColor = color;
85
       selectedButtonColor = color;
84
     }
86
     }
85
-    
86
     NSString *tabBarSelectedButtonColor = tabsStyle[@"tabBarSelectedButtonColor"];
87
     NSString *tabBarSelectedButtonColor = tabsStyle[@"tabBarSelectedButtonColor"];
87
     if (tabBarSelectedButtonColor)
88
     if (tabBarSelectedButtonColor)
88
     {
89
     {
90
       self.tabBar.tintColor = color;
91
       self.tabBar.tintColor = color;
91
       selectedButtonColor = color;
92
       selectedButtonColor = color;
92
     }
93
     }
93
-    
94
+    NSString *tabBarLabelColor = tabsStyle[@"tabBarLabelColor"];
95
+    if(tabBarLabelColor) {
96
+      UIColor *color = tabBarLabelColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarLabelColor] : nil;
97
+      labelColor = color;
98
+    }
99
+    NSString *tabBarSelectedLabelColor = tabsStyle[@"tabBarSelectedLabelColor"];
100
+    if(tabBarLabelColor) {
101
+      UIColor *color = tabBarSelectedLabelColor != (id)[NSNull null] ? [RCTConvert UIColor:
102
+                                                                        tabBarSelectedLabelColor] : nil;
103
+      selectedLabelColor = color;
104
+    }
94
     NSString *tabBarBackgroundColor = tabsStyle[@"tabBarBackgroundColor"];
105
     NSString *tabBarBackgroundColor = tabsStyle[@"tabBarBackgroundColor"];
95
     if (tabBarBackgroundColor)
106
     if (tabBarBackgroundColor)
96
     {
107
     {
167
       
178
       
168
       viewController.tabBarItem.imageInsets = UIEdgeInsetsMake(top, left, bottom, right);
179
       viewController.tabBarItem.imageInsets = UIEdgeInsetsMake(top, left, bottom, right);
169
     }
180
     }
170
-    
171
     NSMutableDictionary *unselectedAttributes = [RCTHelpers textAttributesFromDictionary:tabsStyle withPrefix:@"tabBarText" baseFont:[UIFont systemFontOfSize:10]];
181
     NSMutableDictionary *unselectedAttributes = [RCTHelpers textAttributesFromDictionary:tabsStyle withPrefix:@"tabBarText" baseFont:[UIFont systemFontOfSize:10]];
172
-    if (!unselectedAttributes[NSForegroundColorAttributeName] && buttonColor) {
173
-      unselectedAttributes[NSForegroundColorAttributeName] = buttonColor;
182
+    if (!unselectedAttributes[NSForegroundColorAttributeName] && labelColor) {
183
+      unselectedAttributes[NSForegroundColorAttributeName] = labelColor;
174
     }
184
     }
175
     
185
     
176
-    [viewController.tabBarItem setTitleTextAttributes:unselectedAttributes forState:UIControlStateNormal]
177
-    ;
186
+    [viewController.tabBarItem setTitleTextAttributes:unselectedAttributes forState:UIControlStateNormal];
178
     
187
     
179
     NSMutableDictionary *selectedAttributes = [RCTHelpers textAttributesFromDictionary:tabsStyle withPrefix:@"tabBarSelectedText" baseFont:[UIFont systemFontOfSize:10]];
188
     NSMutableDictionary *selectedAttributes = [RCTHelpers textAttributesFromDictionary:tabsStyle withPrefix:@"tabBarSelectedText" baseFont:[UIFont systemFontOfSize:10]];
180
-    if (!selectedAttributes[NSForegroundColorAttributeName] && selectedButtonColor) {
181
-      selectedAttributes[NSForegroundColorAttributeName] = selectedButtonColor;
189
+    if (!selectedAttributes[NSForegroundColorAttributeName] && selectedLabelColor) {
190
+      selectedAttributes[NSForegroundColorAttributeName] = selectedLabelColor;
182
     }
191
     }
183
     
192
     
184
     [viewController.tabBarItem setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
193
     [viewController.tabBarItem setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];