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,6 +20,9 @@ Navigation.startTabBasedApp({
20 20
   tabBarBackgroundColor: '#551A8B' // change the background color of the tab bar
21 21
   tabBarTranslucent: false // change the translucent of the tab bar to false
22 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 26
   tabBarHideShadow: true // iOS only. Remove default tab bar top shadow (hairline)
24 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,6 +71,8 @@
71 71
   
72 72
   UIColor *buttonColor = nil;
73 73
   UIColor *selectedButtonColor = nil;
74
+  UIColor *labelColor = nil;
75
+  UIColor *selectedLabelColor = nil;
74 76
   NSDictionary *tabsStyle = props[@"style"];
75 77
   if (tabsStyle)
76 78
   {
@@ -82,7 +84,6 @@
82 84
       buttonColor = color;
83 85
       selectedButtonColor = color;
84 86
     }
85
-    
86 87
     NSString *tabBarSelectedButtonColor = tabsStyle[@"tabBarSelectedButtonColor"];
87 88
     if (tabBarSelectedButtonColor)
88 89
     {
@@ -90,7 +91,17 @@
90 91
       self.tabBar.tintColor = color;
91 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 105
     NSString *tabBarBackgroundColor = tabsStyle[@"tabBarBackgroundColor"];
95 106
     if (tabBarBackgroundColor)
96 107
     {
@@ -167,18 +178,16 @@
167 178
       
168 179
       viewController.tabBarItem.imageInsets = UIEdgeInsetsMake(top, left, bottom, right);
169 180
     }
170
-    
171 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 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 193
     [viewController.tabBarItem setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];