20
20
static const NSString *kDetail = @" detail" ;
21
21
static const NSString *kIconName = @" icon" ;
22
22
23
+ // / Describes the effective style of the view controller.
24
+ typedef NS_ENUM (NSUInteger , MTZWhatsNewViewControllerEffectiveStyle) {
25
+ // / Describes a view controller with light text and content.
26
+ MTZWhatsNewViewControllerEffectiveStyleLightContent,
27
+ // / Describes a view controller with dark text and content.
28
+ MTZWhatsNewViewControllerEffectiveStyleDarkContent,
29
+ };
30
+
23
31
@interface MTZWhatsNewViewController () <UICollectionViewDelegate, UICollectionViewDataSource>
24
32
25
33
// / An ordered list of the versions from newest to oldest.
@@ -34,6 +42,9 @@ @interface MTZWhatsNewViewController () <UICollectionViewDelegate, UICollectionV
34
42
// / The button to dismiss the view controller.
35
43
@property (strong , nonatomic ) UIButton *dismissButton;
36
44
45
+ // / The effective style to use when @c style is @c MTZWhatsNewViewControllerStyleAutomatic.
46
+ @property (nonatomic ) MTZWhatsNewViewControllerEffectiveStyle effectiveStyle;
47
+
37
48
@end
38
49
39
50
@implementation MTZWhatsNewViewController
@@ -113,7 +124,6 @@ - (void)commonInit
113
124
self.collectionView .contentInset = edgeInsets;
114
125
self.collectionView .backgroundColor = [UIColor clearColor ];
115
126
self.collectionView .scrollIndicatorInsets = edgeInsets;
116
- self.collectionView .indicatorStyle = UIScrollViewIndicatorStyleWhite;
117
127
118
128
// Get Started.
119
129
UIToolbar *buttonBackground = [[UIToolbar alloc ] init ];
@@ -130,6 +140,10 @@ - (void)commonInit
130
140
self.dismissButton .titleLabel .font = buttonFont;
131
141
[self .dismissButton addTarget: self action: @selector (didTapContinueButton: ) forControlEvents: UIControlEventTouchUpInside];
132
142
143
+ // Defaults.
144
+ self.backgroundGradientTopColor = [UIColor blackColor ];
145
+ self.backgroundGradientBottomColor = [UIColor blackColor ];
146
+ self.style = MTZWhatsNewViewControllerStyleAutomatic;
133
147
self.dismissButtonText = NSLocalizedString(@" Get Started" , nil );
134
148
}
135
149
@@ -166,6 +180,36 @@ - (void)setFeatures:(NSDictionary *)features
166
180
[self .collectionView reloadData ];
167
181
}
168
182
183
+ - (void )setStyle : (MTZWhatsNewViewControllerStyle)style
184
+ {
185
+ _style = style;
186
+
187
+ switch (_style) {
188
+ case MTZWhatsNewViewControllerStyleLightContent:
189
+ _effectiveStyle = MTZWhatsNewViewControllerEffectiveStyleDarkContent;
190
+ break ;
191
+ case MTZWhatsNewViewControllerStyleDarkContent:
192
+ _effectiveStyle = MTZWhatsNewViewControllerEffectiveStyleLightContent;
193
+ break ;
194
+ case MTZWhatsNewViewControllerStyleAutomatic:
195
+ default :
196
+ _effectiveStyle = [self appropriateStyleForBackgroundColor: [self backgroundGradientTopColor ]];
197
+ break ;
198
+ }
199
+
200
+ // Reload collection view to change styles.
201
+ [self .collectionView reloadData ];
202
+
203
+ switch (_effectiveStyle) {
204
+ case MTZWhatsNewViewControllerEffectiveStyleDarkContent:
205
+ self.collectionView .indicatorStyle = UIScrollViewIndicatorStyleBlack;
206
+ break ;
207
+ case MTZWhatsNewViewControllerEffectiveStyleLightContent:
208
+ self.collectionView .indicatorStyle = UIScrollViewIndicatorStyleWhite;
209
+ break ;
210
+ }
211
+ }
212
+
169
213
- (void )setBackgroundGradientTopColor : (UIColor *)topColor
170
214
{
171
215
_backgroundGradientTopColor = [topColor copy ];
@@ -185,6 +229,32 @@ - (void)setDismissButtonText:(NSString *)dismissButtonText
185
229
}
186
230
187
231
232
+ #pragma mark - Style
233
+
234
+ - (MTZWhatsNewViewControllerEffectiveStyle)appropriateStyleForBackgroundColor : (UIColor *)backgroundColor
235
+ {
236
+ CGFloat r, g, b, a;
237
+ [backgroundColor getRed: &r green: &g blue: &b alpha: &a];
238
+
239
+ // Equation from http://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color/596243#596243
240
+ CGFloat perception = 1 .0f - ((0 .299f * r) + (0 .587f * g) + (0 .114f * b));
241
+
242
+ if ( perception < 0.5 ) {
243
+ return MTZWhatsNewViewControllerEffectiveStyleDarkContent;
244
+ } else {
245
+ return MTZWhatsNewViewControllerEffectiveStyleLightContent;
246
+ }
247
+ }
248
+
249
+ - (UIColor *)contentColor
250
+ {
251
+ switch (_effectiveStyle) {
252
+ case MTZWhatsNewViewControllerEffectiveStyleLightContent: return [UIColor whiteColor ];
253
+ case MTZWhatsNewViewControllerEffectiveStyleDarkContent: return [UIColor blackColor ];
254
+ }
255
+ }
256
+
257
+
188
258
#pragma mark - UICollectionViewDelegateFlowLayout
189
259
190
260
- (CGSize )collectionView : (UICollectionView *)collectionView layout : (UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection : (NSInteger )section
@@ -241,7 +311,7 @@ - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
241
311
label.translatesAutoresizingMaskIntoConstraints = NO ;
242
312
[view addConstraints: [NSLayoutConstraint constraintsToStretchHorizontallyToSuperview: label]];
243
313
label.text = NSLocalizedString(@" What’s New" , nil );
244
- label.textColor = [UIColor whiteColor ];
314
+ label.textColor = [self contentColor ];
245
315
label.textAlignment = NSTextAlignmentCenter;
246
316
247
317
// Larger font and divider.
@@ -259,7 +329,7 @@ - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
259
329
[divider addConstraint: [NSLayoutConstraint constraintToSetStaticHeight: 0.5 toView: divider]];
260
330
[view addConstraint: [NSLayoutConstraint constraintWithItem: divider attribute: NSLayoutAttributeCenterX relatedBy: NSLayoutRelationEqual toItem: label attribute: NSLayoutAttributeCenterX multiplier: 1.0 constant: 0.0 ]];
261
331
[view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @" V:[label][divider]" options: NSLayoutFormatDirectionLeftToRight metrics: nil views: @{@" label" : label, @" divider" : divider}]];
262
- divider.backgroundColor = [UIColor colorWithWhite: 1.0 alpha :0.75 ];
332
+ divider.backgroundColor = [[ self contentColor ] colorWithAlphaComponent :0 .75f ];
263
333
} else {
264
334
label.font = [UIFont fontWithName: @" HelveticaNeue-Thin" size: 30 ];
265
335
}
@@ -280,7 +350,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
280
350
if ( iconName ) {
281
351
cell.icon = [UIImage imageNamed: iconName];
282
352
}
283
-
353
+ cell. contentColor = [ self contentColor ];
284
354
cell.layoutStyle = [self shouldUseGridLayout ] ? MTZWhatsNewFeatureCollectionViewCellLayoutStyleGrid : MTZWhatsNewFeatureCollectionViewCellLayoutStyleList;
285
355
286
356
return cell;
0 commit comments