[MDCButton] Added new API for coloring image tint color for state.
Examples updated.
Unit Test Written.
![screen shot 2018-04-20 at 1 14 17 pm](https://user-images.githubusercontent.com/36271115/39064922-27f71680-449e-11e8-99fb-9fcd0e69d870.png)
![screen shot 2018-04-20 at 1 14 21 pm](https://user-images.githubusercontent.com/36271115/39064929-2be07ffc-449e-11e8-820b-7812cbb8d382.png)
MODIFY
components/Buttons/examples/ButtonsShapesExampleViewController.m
components/Buttons/examples/ButtonsShapesExampleViewController.m
UIImage *plusImage = [UIImage imageNamed:@"Plus"];
plusImage = [plusImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[raisedButton setImage:plusImage forState:UIControlStateNormal];
- [raisedButton setTitleColor:
- [UIColor colorWithRed:71/255.0f green:50/255.0f blue:50/255.0f alpha:1]
- forState:UIControlStateNormal];
- raisedButton.imageView.tintColor =
- [UIColor colorWithRed:71/255.0f green:50/255.0f blue:50/255.0f alpha:1];
+ UIColor *titleAndImageColor = [UIColor colorWithRed:71/255.0f green:50/255.0f blue:50/255.0f alpha:1];
+ [raisedButton setTitleColor:titleAndImageColor forState:UIControlStateNormal];
+ [raisedButton setImageTintColor:titleAndImageColor forState:UIControlStateNormal];
[raisedButton setBackgroundColor:
[UIColor colorWithRed:242/255.0f green:174/255.0f blue:178/255.0f alpha:1]];
self.floatingButton = [[MDCFloatingButton alloc] init];
[self.floatingButton setTitleColor:titleColor forState:UIControlStateNormal];
- self.floatingButton.imageView.tintColor = UIColor.whiteColor;
+ [self.floatingButton setImageTintColor:UIColor.whiteColor forState:UIControlStateNormal];
[self.floatingButton setImage:plusImage forState:UIControlStateNormal];
[self.floatingButton sizeToFit];

MODIFY
components/Buttons/examples/ButtonsTypicalUseExampleViewController.m
components/Buttons/examples/ButtonsTypicalUseExampleViewController.m
[[UIImage imageNamed:@"Plus"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[self.floatingButton setImage:plusImage forState:UIControlStateNormal];
[MDCFloatingButtonColorThemer applySemanticColorScheme:colorScheme toButton:self.floatingButton];
- [self.floatingButton setTintColor:colorScheme.onSecondaryColor];
+ [self.floatingButton setImageTintColor:colorScheme.onSecondaryColor
+ forState:UIControlStateNormal];
[self.view addSubview:self.floatingButton];
self.buttons = @[

MODIFY
components/Buttons/examples/FloatingButtonExampleViewController.m
components/Buttons/examples/FloatingButtonExampleViewController.m
inMode:MDCFloatingButtonModeExpanded];
[MDCFloatingButtonColorThemer applySemanticColorScheme:colorScheme
toButton:self.miniFloatingButton];
- [self.miniFloatingButton setTintColor:colorScheme.onSecondaryColor];
+ [self.miniFloatingButton setImageTintColor:colorScheme.onSecondaryColor
+ forState:UIControlStateNormal];
self.defaultFloatingButton = [[MDCFloatingButton alloc] init];
[self.defaultFloatingButton setImage:plusImage forState:UIControlStateNormal];
[MDCFloatingButtonColorThemer applySemanticColorScheme:colorScheme
toButton:self.defaultFloatingButton];
- [self.defaultFloatingButton setTintColor:colorScheme.onSecondaryColor];
+ [self.defaultFloatingButton setImageTintColor:colorScheme.onSecondaryColor
+ forState:UIControlStateNormal];
self.largeIconFloatingButton = [[MDCFloatingButton alloc] init];
self.largeIconFloatingButton.translatesAutoresizingMaskIntoConstraints = NO;
inMode:MDCFloatingButtonModeExpanded];
[MDCFloatingButtonColorThemer applySemanticColorScheme:colorScheme
toButton:self.largeIconFloatingButton];
- [self.largeIconFloatingButton setTintColor:colorScheme.onSecondaryColor];
+ [self.largeIconFloatingButton setImageTintColor:colorScheme.onSecondaryColor
+ forState:UIControlStateNormal];
[self.view addSubview:self.iPadLabel];
[self.view addSubview:self.miniFloatingButton];

MODIFY
components/Buttons/src/MDCButton.h
components/Buttons/src/MDCButton.h
- (void)setBorderColor:(nullable UIColor *)borderColor forState:(UIControlState)state
UI_APPEARANCE_SELECTOR;
+/**
+ A color used as the button's imageView tint color @c imageTintColor for @c state.
+
+ If no image tint color has been set for a given state, the returned value will fall back to the
+ value set for UIControlStateNormal.
+
+ @param state The state.
+ @return The tint color.
+ */
+- (nullable UIColor *)imageTintColorForState:(UIControlState)state;
+
+/**
+ Sets the image view tint color for a particular control state.
+
+ If left unset or reset to nil for a given state, it falls back to UIControlStateNormal setting.
+
+ @param imageTintColor The imageView tint color to set.
+ @param state The state to set.
+ */
+- (void)setImageTintColor:(nullable UIColor *)imageTintColor forState:(UIControlState)state;
+
+
/**
The value set for the button's @c borderWidth for @c state.

MODIFY
components/Buttons/src/MDCButton.m
components/Buttons/src/MDCButton.m
@@ -115,10 +115,12 @@ static NSAttributedString *uppercaseAttributedString(NSAttributedString *string)
NSMutableDictionary<NSNumber *, UIColor *> *_borderColors;
NSMutableDictionary<NSNumber *, NSNumber *> *_borderWidths;
NSMutableDictionary<NSNumber *, UIColor *> *_shadowColors;
+ NSMutableDictionary<NSNumber *, UIColor *> *_imageTintColors;
NSMutableDictionary<NSNumber *, UIFont *> *_fonts;
CGFloat _enabledAlpha;
BOOL _hasCustomDisabledTitleColor;
+ BOOL _imageTintStatefulAPIEnabled;
// Cached accessibility settings.
NSMutableDictionary<NSNumber *, NSString *> *_nontransformedTitles;
@@ -296,6 +298,7 @@ static NSAttributedString *uppercaseAttributedString(NSAttributedString *string)
_userElevations = [NSMutableDictionary dictionary];
_nontransformedTitles = [NSMutableDictionary dictionary];
_borderColors = [NSMutableDictionary dictionary];
+ _imageTintColors = [NSMutableDictionary dictionary];
_borderWidths = [NSMutableDictionary dictionary];
_fonts = [NSMutableDictionary dictionary];
@@ -510,6 +513,7 @@ static NSAttributedString *uppercaseAttributedString(NSAttributedString *string)
[self updateBorderWidth];
[self updateShadowColor];
[self updateTitleFont];
+ [self updateImageTintColor];
}
#pragma mark - Title Uppercasing
@@ -689,6 +693,26 @@ static NSAttributedString *uppercaseAttributedString(NSAttributedString *string)
[self updateAlphaAndBackgroundColorAnimated:NO];
}
+#pragma mark - Image Tint Color
+
+- (nullable UIColor *)imageTintColorForState:(UIControlState)state {
+
+ return _imageTintColors[@(state)] ?: _imageTintColors[@(UIControlStateNormal)];
+}
+
+- (void)setImageTintColor:(nullable UIColor *)imageTintColor forState:(UIControlState)state {
+ _imageTintColors[@(state)] = imageTintColor;
+ _imageTintStatefulAPIEnabled = YES;
+ [self updateImageTintColor];
+}
+
+- (void)updateImageTintColor {
+ if (!_imageTintStatefulAPIEnabled) {
+ return;
+ }
+ self.imageView.tintColor = [self imageTintColorForState:self.state];
+}
+
#pragma mark - Elevations
- (CGFloat)elevationForState:(UIControlState)state {

MODIFY
components/Buttons/tests/unit/ButtonsTests.m
components/Buttons/tests/unit/ButtonsTests.m
@@ -366,6 +366,91 @@ static NSString *controlStateDescription(UIControlState controlState) {
}
}
+#pragma mark - imageTintColor:forState:
+
+- (void)testRemovedImageTintColorForState {
+ // Given
+ MDCButton *button = [[MDCButton alloc] init];
+
+ // When
+ [button setImageTintColor:nil forState:UIControlStateNormal];
+
+ // Then
+ XCTAssertNil([button imageTintColorForState:UIControlStateNormal]);
+ XCTAssertNil([button imageTintColorForState:UIControlStateHighlighted]);
+}
+
+- (void)testDefaultImageTintColorForState {
+ // Given
+ MDCButton *button = [[MDCButton alloc] init];
+
+ // Then
+ XCTAssertNil([button imageTintColorForState:UIControlStateSelected]);
+}
+
+- (void)testImageTintForUnspecifiedStateEqualsNormalState {
+ // Given
+ MDCButton *button = [[MDCButton alloc] init];
+ UIColor *color = randomColor();
+
+ // When
+ [button setImageTintColor:color forState:UIControlStateNormal];
+
+ XCTAssertEqual([button imageTintColorForState:UIControlStateHighlighted], color);
+}
+
+- (void)testImageTintColorForState {
+ // Given
+ MDCButton *button = [[MDCButton alloc] init];
+
+ for (NSUInteger controlState = 0; controlState < kNumUIControlStates; ++controlState) {
+ // And given
+ UIColor *color = randomColor();
+
+ // When
+ [button setImageTintColor:color forState:controlState];
+
+ // Then
+ XCTAssertEqualObjects([button imageTintColorForState:controlState], color);
+ }
+}
+
+- (void)testImageTintColorForStateFallsBackToDefault {
+ // Given
+ MDCButton *button = [[MDCButton alloc] init];
+ UIColor *normalTint = [UIColor yellowColor];
+ UIColor *selectedTint = [UIColor redColor];
+
+ // When
+ [button setImageTintColor:normalTint forState:UIControlStateNormal];
+ [button setImageTintColor:selectedTint forState:UIControlStateSelected];
+
+ // Then
+ XCTAssertEqualObjects([button imageTintColorForState:UIControlStateNormal], normalTint);
+ XCTAssertEqualObjects([button imageTintColorForState:UIControlStateSelected], selectedTint);
+ XCTAssertEqualObjects([button imageTintColorForState:UIControlStateHighlighted], normalTint);
+}
+
+- (void)testImageTintColorForStateSetsImageViewTintColor {
+ // Given
+ MDCButton *button = [[MDCButton alloc] init];
+ UIColor *normalTint = [UIColor yellowColor];
+ UIColor *selectedTint = [UIColor redColor];
+
+ // When
+ [button setImageTintColor:normalTint forState:UIControlStateNormal];
+ [button setImageTintColor:selectedTint forState:UIControlStateSelected];
+
+ // Then
+ XCTAssertEqualObjects(button.imageView.tintColor, normalTint);
+
+ // When
+ button.selected = YES;
+
+ // Then
+ XCTAssertEqualObjects(button.imageView.tintColor, selectedTint);
+}
+
#pragma mark - backgroundColor:forState:
- (void)testCurrentBackgroundColorNormal {

[MDCButton] Added new API for coloring image tint color for state. (#3423)

Examples updated.
Unit Test Written.
![screen shot 2018-04-20 at 1 14 17 pm](https://user-images.githubusercontent.com/36271115/39064922-27f71680-449e-11e8-99fb-9fcd0e69d870.png)
![screen shot 2018-04-20 at 1 14 21 pm](https://user-images.githubusercontent.com/36271115/39064929-2be07ffc-449e-11e8-820b-7812cbb8d382.png)