Skip to content

Commit e19726d

Browse files
committed
Automatically determining effective style based on background color.
1 parent 6d05711 commit e19726d

4 files changed

+92
-9
lines changed

Classes/MTZWhatsNewViewController/MTZWhatsNewFeatureCollectionViewCell.h

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ typedef NS_ENUM(NSUInteger, MTZWhatsNewFeatureCollectionViewCellLayoutStyle){
2727
/// An image represeting the feature.
2828
@property (nonatomic, copy) UIImage *icon;
2929

30+
/// The color to use for the content.
31+
@property (nonatomic, copy) UIColor *contentColor;
32+
3033
/// The style of the layout.
3134
@property (nonatomic) MTZWhatsNewFeatureCollectionViewCellLayoutStyle layoutStyle;
3235

Classes/MTZWhatsNewViewController/MTZWhatsNewFeatureCollectionViewCell.m

+11-1
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,22 @@ - (NSString *)detail
165165

166166
- (void)setIcon:(UIImage *)icon
167167
{
168-
self.imageView.image = [icon copy];
168+
#warning Should this always be template? What if the icon isn't meant for template?
169+
self.imageView.image = [icon imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
169170
}
170171

171172
- (UIImage *)icon
172173
{
173174
return self.imageView.image;
174175
}
175176

177+
- (void)setContentColor:(UIColor *)contentColor
178+
{
179+
_contentColor = contentColor;
180+
181+
self.textLabel.textColor = _contentColor;
182+
self.detailTextLabel.textColor = _contentColor;
183+
self.imageView.tintColor = _contentColor;
184+
}
185+
176186
@end

Classes/MTZWhatsNewViewController/MTZWhatsNewViewController.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010

1111
/// Describes the style of the view controller.
1212
typedef NS_ENUM(NSUInteger, MTZWhatsNewViewControllerStyle) {
13+
/// Describes a view controller that automatically determines whether to use light or dark text.
14+
MTZWhatsNewViewControllerStyleAutomatic,
1315
/// Describes a view controller with light text and content.
1416
MTZWhatsNewViewControllerStyleLightContent,
1517
/// Describes a view controller with dark text and content.
16-
MTZWhatsNewViewControllerStyleDarkContent
18+
MTZWhatsNewViewControllerStyleDarkContent,
1719
};
1820

1921
@interface MTZWhatsNewViewController : UIViewController
@@ -83,10 +85,8 @@ typedef NS_ENUM(NSUInteger, MTZWhatsNewViewControllerStyle) {
8385

8486
#pragma mark - Appearance Customization
8587

86-
#warning Option for scrollview or wordcloud?
87-
8888
/// The style of what's new view controller.
89-
/// Default is @c MTZWhatsNewViewControllerStyleLightContent.
89+
/// Default is @c MTZWhatsNewViewControllerStyleAutomatic.
9090
@property (nonatomic) MTZWhatsNewViewControllerStyle style;
9191

9292
/// The color to display on the top of the background gradient.

Classes/MTZWhatsNewViewController/MTZWhatsNewViewController.m

+74-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@
2020
static const NSString *kDetail = @"detail";
2121
static const NSString *kIconName = @"icon";
2222

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+
2331
@interface MTZWhatsNewViewController () <UICollectionViewDelegate, UICollectionViewDataSource>
2432

2533
/// An ordered list of the versions from newest to oldest.
@@ -34,6 +42,9 @@ @interface MTZWhatsNewViewController () <UICollectionViewDelegate, UICollectionV
3442
/// The button to dismiss the view controller.
3543
@property (strong, nonatomic) UIButton *dismissButton;
3644

45+
/// The effective style to use when @c style is @c MTZWhatsNewViewControllerStyleAutomatic.
46+
@property (nonatomic) MTZWhatsNewViewControllerEffectiveStyle effectiveStyle;
47+
3748
@end
3849

3950
@implementation MTZWhatsNewViewController
@@ -113,7 +124,6 @@ - (void)commonInit
113124
self.collectionView.contentInset = edgeInsets;
114125
self.collectionView.backgroundColor = [UIColor clearColor];
115126
self.collectionView.scrollIndicatorInsets = edgeInsets;
116-
self.collectionView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
117127

118128
// Get Started.
119129
UIToolbar *buttonBackground = [[UIToolbar alloc] init];
@@ -130,6 +140,10 @@ - (void)commonInit
130140
self.dismissButton.titleLabel.font = buttonFont;
131141
[self.dismissButton addTarget:self action:@selector(didTapContinueButton:) forControlEvents:UIControlEventTouchUpInside];
132142

143+
// Defaults.
144+
self.backgroundGradientTopColor = [UIColor blackColor];
145+
self.backgroundGradientBottomColor = [UIColor blackColor];
146+
self.style = MTZWhatsNewViewControllerStyleAutomatic;
133147
self.dismissButtonText = NSLocalizedString(@"Get Started", nil);
134148
}
135149

@@ -166,6 +180,36 @@ - (void)setFeatures:(NSDictionary *)features
166180
[self.collectionView reloadData];
167181
}
168182

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+
169213
- (void)setBackgroundGradientTopColor:(UIColor *)topColor
170214
{
171215
_backgroundGradientTopColor = [topColor copy];
@@ -185,6 +229,32 @@ - (void)setDismissButtonText:(NSString *)dismissButtonText
185229
}
186230

187231

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+
188258
#pragma mark - UICollectionViewDelegateFlowLayout
189259

190260
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
@@ -241,7 +311,7 @@ - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
241311
label.translatesAutoresizingMaskIntoConstraints = NO;
242312
[view addConstraints:[NSLayoutConstraint constraintsToStretchHorizontallyToSuperview:label]];
243313
label.text = NSLocalizedString(@"What’s New", nil);
244-
label.textColor = [UIColor whiteColor];
314+
label.textColor = [self contentColor];
245315
label.textAlignment = NSTextAlignmentCenter;
246316

247317
// Larger font and divider.
@@ -259,7 +329,7 @@ - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
259329
[divider addConstraint:[NSLayoutConstraint constraintToSetStaticHeight:0.5 toView:divider]];
260330
[view addConstraint:[NSLayoutConstraint constraintWithItem:divider attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:label attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
261331
[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];
263333
} else {
264334
label.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:30];
265335
}
@@ -280,7 +350,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
280350
if ( iconName ) {
281351
cell.icon = [UIImage imageNamed:iconName];
282352
}
283-
353+
cell.contentColor = [self contentColor];
284354
cell.layoutStyle = [self shouldUseGridLayout] ? MTZWhatsNewFeatureCollectionViewCellLayoutStyleGrid : MTZWhatsNewFeatureCollectionViewCellLayoutStyleList;
285355

286356
return cell;

0 commit comments

Comments
 (0)