Skip to content

Commit 9b28083

Browse files
committed
Fixes and changes.
1 parent 8e597d8 commit 9b28083

File tree

5 files changed

+42
-30
lines changed

5 files changed

+42
-30
lines changed

components/Buttons/Buttons/ViewController.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ - (void)viewDidLoad {
2424
[super viewDidLoad];
2525
self.view.backgroundColor = [UIColor whiteColor];
2626

27-
// Load your Material Component here.
2827
MDCRaisedButton *raisedButton = [MDCRaisedButton new];
2928
[raisedButton setTitle:@"Raised Button" forState:UIControlStateNormal];
3029
[raisedButton sizeToFit];
@@ -82,7 +81,7 @@ - (void)viewDidLoad {
8281
}
8382

8483
- (void)didTap:(id)sender {
85-
NSLog(@"Did tap %@!", NSStringFromClass([sender class]));
84+
NSLog(@"%@ was tapped.", NSStringFromClass([sender class]));
8685
}
8786

8887
@end

components/Buttons/ButtonsTests/ButtonsTests.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ - (void)testCurrentBackgroundColor {
161161

162162
// Then
163163
XCTAssertEqual(button.state, controlState);
164-
XCTAssertEqualObjects(button.currentBackgroundColor, color);
164+
XCTAssertEqualObjects([button backgroundColorForState:button.state], color);
165165
}
166166
}
167167

@@ -179,7 +179,7 @@ - (void)testCurrentBackgroundColorFallbackToNormal {
179179

180180
// Then
181181
XCTAssertEqual(button.state, controlState);
182-
XCTAssertEqualObjects(button.currentBackgroundColor, color);
182+
XCTAssertEqualObjects([button backgroundColorForState:button.state], color);
183183
}
184184

185185
- (void)testInkColors {
@@ -204,7 +204,7 @@ - (void)testDefaultColors {
204204
// Colors chosen from: https://www.google.com/design/spec/style/color.html#color-color-palette
205205
UIColor *blue500 = MDCColorFromRGB(0x2196F3);
206206
UIColor *blue300 = MDCColorFromRGB(0x64B5F6);
207-
XCTAssertEqualObjects([button currentBackgroundColor], blue500);
207+
XCTAssertEqualObjects([button backgroundColorForState:button.state], blue500);
208208
UIColor *lightBlueInk = [blue300 colorWithAlphaComponent:0.25f];
209209
XCTAssertEqualObjects(button.inkColor, lightBlueInk);
210210
}

components/Buttons/src/MDCButton.h

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,42 @@
1717
#import <CoreGraphics/CoreGraphics.h>
1818
#import <UIKit/UIKit.h>
1919

20-
@interface MDCButton : UIButton
21-
2220
/**
23-
* The background color given this UIControl's @c state.
24-
* @c setBackgroundColorForState: to set the valid background colors for a given state.
21+
A Material flat, raised or floating button.
22+
23+
All buttons display animated ink splashes when the user interacts with the button.
24+
25+
The title color of the button set to have an accessible contrast ratio with the button's
26+
background color. To ensure this works for flat buttons (with transparent background), the caller
27+
is responsible for setting (and updating, if necessary) the button's underlyingColor property.
28+
29+
All buttons set the exclusiveTouch property to YES by default, which prevents users from
30+
simultaneously interacting with a button and other UI elements.
31+
32+
@see http://www.google.com/design/spec/components/buttons.html#buttons-main-buttons
2533
*/
26-
@property(nullable, nonatomic, readonly, strong) UIColor *currentBackgroundColor;
34+
@interface MDCButton : UIButton
2735

2836
/**
29-
* A color used as the button's @c backgroundColor.
30-
* If left unset or reset to nil for a given state, then an appropriate default color is used.
37+
A color used as the button's @c backgroundColor.
38+
The default value is nil, which results in a transparent background color.
39+
40+
@param state The state.
41+
@return The background color.
3142
*/
3243
- (nonnull UIColor *)backgroundColorForState:(UIControlState)state;
3344

3445
/**
35-
* A color used as the button's @c backgroundColor.
36-
*
37-
* @param elevation The elevation to set.
38-
* @param state The state to set.
46+
A color used as the button's @c backgroundColor.
47+
48+
@param backgroundColor The background color.
49+
@param state The state.
3950
*/
4051
- (void)setBackgroundColor:(nullable UIColor *)backgroundColor forState:(UIControlState)state;
4152

42-
/**
43-
The ink color of the button.
44-
If left unset or reset to nil an appropriate default color is used.
45-
*/
53+
/** The ink color of the button. */
4654
@property(null_resettable, nonatomic, strong) UIColor *inkColor;
4755

48-
///** Please use @c backgroundColorForState: instead. */
49-
//- (void)setBackgroundColor:(nullable UIColor *)backgroundColor NS_UNAVAILABLE;
50-
//- (nullable UIColor *)backgroundColor NS_UNAVAILABLE;
51-
5256
/**
5357
* A custom title color for the non-disabled states. The default is nil, which means that the button
5458
* chooses its title color automatically based on |underlyingColor|, whether the button is opaque,

components/Buttons/src/MDCButton.m

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,22 @@
4141
static NSString *const MDCButtonUserElevationsKey = @"MDCButtonUserElevationsKey";
4242

4343
// MDCButtonUserZIndicesKey provides backward compatibility with old z-index shadows values.
44+
// TODO: Remove from MDC, it is only useful for internal clients.
4445
static NSString *const MDCButtonUserZIndicesKey = @"MDCButtonUserZIndicesKey";
4546

4647
static const NSTimeInterval MDCButtonAnimationDuration = 0.2;
4748

4849
// http://www.google.com/design/spec/components/buttons.html#buttons-main-buttons
4950
static const CGFloat MDCButtonDisabledAlpha = 0.1f;
51+
static const uint32_t MDCButtonDefaultBackgroundColor = 0x2196F3;
5052

5153
// Checks whether the provided floating point number is exactly zero.
5254
static inline BOOL MDCButtonFloatIsExactlyZero(CGFloat value) {
5355
return (value == 0.f);
5456
}
5557

56-
static inline UIColor *MDCColorFromRGB(NSInteger rgbValue) {
58+
// Creates a UIColor from a 24-bit RGB color encoded as an integer.
59+
static inline UIColor *MDCColorFromRGB(uint32_t rgbValue) {
5760
return [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16)) / 255.0
5861
green:((float)((rgbValue & 0x00FF00) >> 8)) / 255.0
5962
blue:((float)((rgbValue & 0x0000FF) >> 0)) / 255.0
@@ -134,9 +137,11 @@ - (void)commonInit {
134137
// Block users from activating multiple buttons simultaneously by default.
135138
self.exclusiveTouch = YES;
136139

137-
//default background colors
138-
[self setBackgroundColor:MDCColorFromRGB(0x2196F3) forState:UIControlStateNormal];
139-
self.inkColor = [MDCColorFromRGB(0x64B5F6) colorWithAlphaComponent:0.25f];
140+
// Default background colors.
141+
[self setBackgroundColor:MDCColorFromRGB(MDCButtonDefaultBackgroundColor)
142+
forState:UIControlStateNormal];
143+
144+
self.inkColor = [UIColor colorWithWhite:1 alpha:CGColorGetAlpha(self.inkView.inkColor.CGColor)];
140145
}
141146

142147
- (void)dealloc {
@@ -455,6 +460,8 @@ - (CGFloat)elevationForState:(UIControlState)state {
455460
}
456461
if (state & UIControlStateSelected) {
457462
CGFloat elevationForNormal = [self elevationForState:UIControlStateNormal];
463+
// TODO(ajsecord): Why is this using the raised button values for all buttons?
464+
// This should just be a factor of 2, I believe.
458465
if (MDCButtonFloatIsExactlyZero(elevationForNormal)) {
459466
return MDCShadowElevationRaisedButtonPressed;
460467
} else {
@@ -482,8 +489,11 @@ - (void)setElevation:(CGFloat)elevation forState:(UIControlState)state {
482489

483490
- (void)resetElevationForState:(UIControlState)state {
484491
[_userElevations removeObjectForKey:@(state)];
492+
[self shadowLayer].elevation = [self elevationForState:self.state];
485493
}
486494

495+
#pragma mark - Private methods
496+
487497
- (UIColor *)currentBackgroundColor {
488498
UIColor *color = _backgroundColors[@(self.state)];
489499
if (color) {
@@ -492,8 +502,6 @@ - (UIColor *)currentBackgroundColor {
492502
return [self backgroundColorForState:UIControlStateNormal];
493503
}
494504

495-
#pragma mark - Private methods
496-
497505
/**
498506
* The background color that a user would see for this button. If self.backgroundColor is not
499507
* transparent, then returns that. Otherwise, returns self.underlyingColor.

components/Buttons/src/MDCFlatButton.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ - (instancetype)initWithFrame:(CGRect)frame {
3131
if (self) {
3232
self.shouldRaiseOnTouch = NO;
3333
self.backgroundColor = nil;
34+
self.inkColor = [UIColor colorWithWhite:0 alpha:CGColorGetAlpha(self.inkColor.CGColor)];
3435
}
3536
return self;
3637
}

0 commit comments

Comments
 (0)