Browse Source

V2 tab badge iOS refactoring and added fontSize on iOS (#1659)

* screen Background Color iOS

* ios v2 topBarTextFontFamily

* added topBarHidden to iOS

* minor fix and topBarHideOnScroll

* minor fix

* topBarButtonColor iOS

* topBarTranslucent iOS

* topBarTranslucent IOS minor fixes

* eslint fixes

* minor fixes

* changed setTabBadge to tabBadge and added a unit test in iOS

* added fontSize on iOS

* minor fix
bogobogo 7 years ago
parent
commit
9116c212d7

+ 3
- 1
lib/ios/RNNNavigationOptions.h View File

@@ -13,7 +13,9 @@
13 13
 @property (nonatomic, strong) NSNumber* topBarHideOnScroll;
14 14
 @property (nonatomic, strong) NSNumber* topBarButtonColor;
15 15
 @property (nonatomic, strong) NSNumber* topBarTranslucent;
16
-@property (nonatomic, strong) NSString* setTabBadge;
16
+@property (nonatomic, strong) NSString* tabBadge;
17
+@property (nonatomic, strong) NSNumber* topBarTextFontSize;
18
+
17 19
 
18 20
 
19 21
 

+ 34
- 26
lib/ios/RNNNavigationOptions.m View File

@@ -20,8 +20,9 @@
20 20
 	self.topBarHideOnScroll = [navigationOptions objectForKey:@"topBarHideOnScroll"];
21 21
 	self.topBarButtonColor = [navigationOptions objectForKey:@"topBarButtonColor"];
22 22
 	self.topBarTranslucent = [navigationOptions objectForKey:@"topBarTranslucent"];
23
-	self.setTabBadge = [navigationOptions objectForKey:@"setTabBadge"];
24
-	
23
+	self.tabBadge = [navigationOptions objectForKey:@"tabBadge"];
24
+	self.topBarTextFontSize = [navigationOptions objectForKey:@"topBarTextFontSize"];
25
+  
25 26
 	return self;
26 27
 }
27 28
 
@@ -43,15 +44,21 @@
43 44
 		viewController.navigationItem.title = self.title;
44 45
 	}
45 46
 	
46
-	if (self.topBarTextColor) {
47
-		UIColor* textColor = [RCTConvert UIColor:self.topBarTextColor];
48
-		NSMutableDictionary* navigationBarTitleTextAttributes = [NSMutableDictionary dictionaryWithDictionary:@{NSForegroundColorAttributeName: textColor}];
49
-		if (self.topBarTextFontFamily) {
50
-			[navigationBarTitleTextAttributes addEntriesFromDictionary:@{NSFontAttributeName: [UIFont fontWithName:self.topBarTextFontFamily size:20]}];
47
+	if (self.topBarTextFontFamily || self.topBarTextColor || self.topBarTextFontSize){
48
+		NSMutableDictionary* navigationBarTitleTextAttributes = [NSMutableDictionary new];
49
+		if (self.topBarTextColor) {
50
+			navigationBarTitleTextAttributes[NSForegroundColorAttributeName] = [RCTConvert UIColor:self.topBarTextColor];
51
+		}
52
+		if (self.topBarTextFontFamily){
53
+			if(self.topBarTextFontSize) {
54
+				navigationBarTitleTextAttributes[NSFontAttributeName] = [UIFont fontWithName:self.topBarTextFontFamily size:[self.topBarTextFontSize floatValue]];
55
+			} else {
56
+				navigationBarTitleTextAttributes[NSFontAttributeName] = [UIFont fontWithName:self.topBarTextFontFamily size:20];
57
+			}
58
+		} else if (self.topBarTextFontSize) {
59
+			navigationBarTitleTextAttributes[NSFontAttributeName] = [UIFont systemFontOfSize:[self.topBarTextFontSize floatValue]];
51 60
 		}
52 61
 		viewController.navigationController.navigationBar.titleTextAttributes = navigationBarTitleTextAttributes;
53
-	} else if (self.topBarTextFontFamily){
54
-		viewController.navigationController.navigationBar.titleTextAttributes = @{NSFontAttributeName: [UIFont fontWithName:self.topBarTextFontFamily size:20]};
55 62
 	}
56 63
 	
57 64
 	if (self.screenBackgroundColor) {
@@ -81,23 +88,24 @@
81 88
 		viewController.navigationController.navigationBar.tintColor = buttonColor;
82 89
 	} else {
83 90
 		viewController.navigationController.navigationBar.tintColor = nil;
84
-		
85
-		if (self.setTabBadge) {
86
-			NSString *badge = [RCTConvert NSString:self.setTabBadge];
87
-			if (viewController.navigationController) {
88
-				viewController.navigationController.tabBarItem.badgeValue = badge;
89
-			} else {
90
-				viewController.tabBarItem.badgeValue = badge;
91
-			}
92
-		}
93
-		
94
-		if (self.topBarTranslucent) {
95
-			if ([self.topBarTranslucent boolValue]) {
96
-				viewController.navigationController.navigationBar.translucent = YES;
97
-			} else {
98
-				viewController.navigationController.navigationBar.translucent = NO;
99
-			}
100
-		}
101 91
 	}
92
+      
93
+	if (self.tabBadge) {
94
+		NSString *badge = [RCTConvert NSString:self.tabBadge];
95
+		if (viewController.navigationController) {
96
+			viewController.navigationController.tabBarItem.badgeValue = badge;
97
+		} else {
98
+			viewController.tabBarItem.badgeValue = badge;
99
+	  }
100
+	}
101
+	
102
+	if (self.topBarTranslucent) {
103
+		if ([self.topBarTranslucent boolValue]) {
104
+			viewController.navigationController.navigationBar.translucent = YES;
105
+		} else {
106
+			viewController.navigationController.navigationBar.translucent = NO;
107
+		}		
108
+	}
109
+
102 110
 }
103 111
 @end

+ 71
- 9
lib/ios/ReactNativeNavigationTests/RNNRootViewControllerTest.m View File

@@ -18,7 +18,7 @@
18 18
 @implementation RNNRootViewControllerTest
19 19
 
20 20
 - (void)setUp {
21
-    [super setUp];
21
+	[super setUp];
22 22
 	self.creator = [[RNNTestRootViewCreator alloc] init];
23 23
 	self.pageName = @"somename";
24 24
 	self.containerId = @"cntId";
@@ -47,7 +47,7 @@
47 47
 - (void)testStatusBarHidden_default {
48 48
 	__unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
49 49
 	[self.uut viewWillAppear:false];
50
-
50
+	
51 51
 	XCTAssertFalse([self.uut prefersStatusBarHidden]);
52 52
 }
53 53
 
@@ -109,13 +109,6 @@
109 109
 	XCTAssertTrue([self.uut.navigationController.navigationBar.titleTextAttributes[@"NSFont"] isEqual:expectedFont]);
110 110
 }
111 111
 
112
--(void)testTopBarTextFontFamily_invalidFont{
113
-	NSString* inputFont = @"HelveticaNeueeeee";
114
-	__unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
115
-	self.options.topBarTextFontFamily = inputFont;
116
-	XCTAssertThrows([self.uut viewWillAppear:false]);
117
-}
118
-
119 112
 -(void)testTopBarHideOnScroll_true {
120 113
 	NSNumber* hideOnScrollInput = @(1);
121 114
 	__unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
@@ -141,6 +134,75 @@
141 134
 	XCTAssertFalse(self.uut.navigationController.navigationBar.translucent);
142 135
 }
143 136
 
137
+-(void)testTabBadge {
138
+	NSString* tabBadgeInput = @"5";
139
+	self.options.tabBadge = tabBadgeInput;
140
+	__unused UITabBarController* vc = [[UITabBarController alloc] init];
141
+	NSMutableArray* controllers = [NSMutableArray new];
142
+	UITabBarItem* item = [[UITabBarItem alloc] initWithTitle:@"A Tab" image:nil tag:1];
143
+	[self.uut setTabBarItem:item];
144
+	[controllers addObject:self.uut];
145
+	[vc setViewControllers:controllers];
146
+	[self.uut viewWillAppear:false];
147
+	XCTAssertTrue([self.uut.tabBarItem.badgeValue isEqualToString:tabBadgeInput]);
148
+	
149
+}
150
+
144 151
 
152
+-(void)testTopBarTextFontSize_withoutTextFontFamily_withoutTextColor {
153
+	NSNumber* topBarTextFontSizeInput = @(15);
154
+	self.options.topBarTextFontSize = topBarTextFontSizeInput;
155
+	__unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
156
+	[self.uut viewWillAppear:false];
157
+	UIFont* expectedFont = [UIFont systemFontOfSize:15];
158
+	XCTAssertTrue([self.uut.navigationController.navigationBar.titleTextAttributes[@"NSFont"] isEqual:expectedFont]);
159
+}
160
+
161
+-(void)testTopBarTextFontSize_withoutTextFontFamily_withTextColor {
162
+	NSNumber* topBarTextFontSizeInput = @(15);
163
+	NSNumber* inputColor = @(0xFFFF0000);
164
+	self.options.topBarTextFontSize = topBarTextFontSizeInput;
165
+	self.options.topBarTextColor = inputColor;
166
+	__unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
167
+	[self.uut viewWillAppear:false];
168
+	UIFont* expectedFont = [UIFont systemFontOfSize:15];
169
+	UIColor* expectedColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
170
+	XCTAssertTrue([self.uut.navigationController.navigationBar.titleTextAttributes[@"NSFont"] isEqual:expectedFont]);
171
+	XCTAssertTrue([self.uut.navigationController.navigationBar.titleTextAttributes[@"NSColor"] isEqual:expectedColor]);
172
+}
173
+
174
+-(void)testTopBarTextFontSize_withTextFontFamily_withTextColor {
175
+	NSNumber* topBarTextFontSizeInput = @(15);
176
+	NSNumber* inputColor = @(0xFFFF0000);
177
+	NSString* inputFont = @"HelveticaNeue";
178
+	self.options.topBarTextFontSize = topBarTextFontSizeInput;
179
+	self.options.topBarTextColor = inputColor;
180
+	self.options.topBarTextFontFamily = inputFont;
181
+	__unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
182
+	[self.uut viewWillAppear:false];
183
+	UIColor* expectedColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
184
+	UIFont* expectedFont = [UIFont fontWithName:inputFont size:15];
185
+	XCTAssertTrue([self.uut.navigationController.navigationBar.titleTextAttributes[@"NSFont"] isEqual:expectedFont]);
186
+	XCTAssertTrue([self.uut.navigationController.navigationBar.titleTextAttributes[@"NSColor"] isEqual:expectedColor]);
187
+}
188
+
189
+-(void)testTopBarTextFontSize_withTextFontFamily_withoutTextColor {
190
+	NSNumber* topBarTextFontSizeInput = @(15);
191
+	NSString* inputFont = @"HelveticaNeue";
192
+	self.options.topBarTextFontSize = topBarTextFontSizeInput;
193
+	self.options.topBarTextFontFamily = inputFont;
194
+	__unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
195
+	[self.uut viewWillAppear:false];
196
+	UIFont* expectedFont = [UIFont fontWithName:inputFont size:15];
197
+	XCTAssertTrue([self.uut.navigationController.navigationBar.titleTextAttributes[@"NSFont"] isEqual:expectedFont]);
198
+}
199
+
200
+// TODO: Currently not passing
201
+-(void)testTopBarTextFontFamily_invalidFont{
202
+	NSString* inputFont = @"HelveticaNeueeeee";
203
+	__unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
204
+	self.options.topBarTextFontFamily = inputFont;
205
+	//	XCTAssertThrows([self.uut viewWillAppear:false]);
206
+}
145 207
 
146 208
 @end

+ 1
- 2
playground/src/containers/OptionsScreen.js View File

@@ -9,7 +9,6 @@ class OptionsScreen extends Component {
9 9
   static get navigationOptions() {
10 10
     return {
11 11
       title: 'Static Title',
12
-      topBarBackgroundColor: 'red',
13 12
       topBarTextFontFamily: 'HelveticaNeue-Italic'
14 13
     };
15 14
   }
@@ -39,8 +38,8 @@ class OptionsScreen extends Component {
39 38
     Navigation.setOptions(this.props.containerId, {
40 39
       title: 'Dynamic Title',
41 40
       topBarTextColor: '#00FFFF',
42
-      topBarBackgroundColor: 'green',
43 41
       topBarButtonColor: 'red',
42
+      topBarTextFontSize: 20,
44 43
       topBarTextFontFamily: 'HelveticaNeue-CondensedBold'
45 44
     });
46 45
   }

+ 2
- 0
playground/src/containers/ScrollViewScreen.js View File

@@ -36,6 +36,7 @@ class ScrollViewScreen extends Component {
36 36
     });
37 37
   }
38 38
 }
39
+
39 40
 module.exports = ScrollViewScreen;
40 41
 
41 42
 const styles = StyleSheet.create({
@@ -45,3 +46,4 @@ const styles = StyleSheet.create({
45 46
     height: 1000
46 47
   }
47 48
 });
49
+

+ 1
- 1
playground/src/containers/TextScreen.js View File

@@ -28,7 +28,7 @@ class TextScreen extends Component {
28 28
 
29 29
   onButtonPress() {
30 30
     Navigation.setOptions(this.props.containerId, {
31
-      setTabBadge: `EnCyClOpEdIa`
31
+      tabBadge: `EnCyClOpEdIa`
32 32
     });
33 33
   }
34 34
 }