36
36
@" MDCButtonDisabledBackgroundColorDarkKey" ;
37
37
static NSString *const MDCButtonInkViewInkColorKey = @" MDCButtonInkViewInkColorKey" ;
38
38
static NSString *const MDCButtonShouldRaiseOnTouchKey = @" MDCButtonShouldRaiseOnTouchKey" ;
39
- static NSString *const MDCButtonUppercaseTitleKey = @" MDCButtonUppercaseTitleKey " ;
39
+ static NSString *const MDCButtonShouldCapitalizeTitleKey = @" MDCButtonShouldCapitalizeTitleKey " ;
40
40
static NSString *const MDCButtonUnderlyingColorKey = @" MDCButtonUnderlyingColorKey" ;
41
41
static NSString *const MDCButtonUserElevationsKey = @" MDCButtonUserElevationsKey" ;
42
42
43
- // MDCButtonUserZIndicesKey provides backward compatibility with old z-index shadows values.
44
- // TODO: Remove from MDC, it is only useful for internal clients.
45
- static NSString *const MDCButtonUserZIndicesKey = @" MDCButtonUserZIndicesKey" ;
46
-
47
43
static const NSTimeInterval MDCButtonAnimationDuration = 0.2 ;
48
44
49
45
// http://www.google.com/design/spec/components/buttons.html#buttons-main-buttons
@@ -96,13 +92,11 @@ - (instancetype)initWithFrame:(CGRect)frame {
96
92
- (void )commonInit {
97
93
_disabledAlpha = MDCButtonDisabledAlpha;
98
94
_shouldRaiseOnTouch = YES ;
99
- _uppercaseTitle = YES ;
95
+ _shouldCapitalizeTitle = YES ;
100
96
_userElevations = [NSMutableDictionary dictionary ];
101
97
_backgroundColors = [NSMutableDictionary dictionary ];
102
98
_accessibilityLabelForState = [NSMutableDictionary dictionary ];
103
99
104
- [self setElevation: MDCShadowElevationNone forState: UIControlStateDisabled];
105
-
106
100
// Disable default highlight state.
107
101
self.adjustsImageWhenHighlighted = NO ;
108
102
self.showsTouchWhenHighlighted = NO ;
@@ -111,12 +105,15 @@ - (void)commonInit {
111
105
self.titleLabel .font = [MDCTypography buttonFont ];
112
106
[self updateTitleColor ];
113
107
[self updateDisabledTitleColor ];
108
+ [self updateAlphaAndBackgroundColorAnimated: NO ];
114
109
115
110
// Default content insets
116
111
self.contentEdgeInsets = [self defaultContentEdgeInsets ];
117
112
118
- self.layer .shadowPath = [self boundingPath ].CGPath ;
119
- self.layer .shadowColor = [UIColor blackColor ].CGColor ;
113
+ MDCShadowLayer *shadowLayer = [self shadowLayer ];
114
+ shadowLayer.shadowPath = [self boundingPath ].CGPath ;
115
+ shadowLayer.shadowColor = [UIColor blackColor ].CGColor ;
116
+ shadowLayer.elevation = [self elevationForState: self .state];
120
117
121
118
// Set up ink layer.
122
119
_inkView = [[MDCInkView alloc ] initWithFrame: self .bounds];
@@ -197,26 +194,18 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder {
197
194
if ([aDecoder containsValueForKey: MDCButtonShouldRaiseOnTouchKey]) {
198
195
self.shouldRaiseOnTouch = [aDecoder decodeBoolForKey: MDCButtonShouldRaiseOnTouchKey];
199
196
}
200
- if ([aDecoder containsValueForKey: MDCButtonUppercaseTitleKey]) {
201
- self.uppercaseTitle = [aDecoder decodeBoolForKey: MDCButtonUppercaseTitleKey];
197
+
198
+ if ([aDecoder containsValueForKey: MDCButtonShouldCapitalizeTitleKey]) {
199
+ self.shouldCapitalizeTitle = [aDecoder decodeBoolForKey: MDCButtonShouldCapitalizeTitleKey];
202
200
}
201
+
203
202
if ([aDecoder containsValueForKey: MDCButtonUnderlyingColorKey]) {
204
203
self.underlyingColor = [aDecoder decodeObjectForKey: MDCButtonUnderlyingColorKey];
205
204
}
206
205
207
206
if ([aDecoder containsValueForKey: MDCButtonUserElevationsKey]) {
208
207
_userElevations = [aDecoder decodeObjectForKey: MDCButtonUserElevationsKey];
209
208
}
210
- // For backward compatibility
211
- if ([aDecoder containsValueForKey: MDCButtonUserZIndicesKey]) {
212
- NSMutableDictionary *userZIndices = [aDecoder decodeObjectForKey: MDCButtonUserZIndicesKey];
213
- NSArray *userZIndicesStates = [userZIndices allKeys ];
214
- for (NSNumber *stateNum in userZIndicesStates) {
215
- NSNumber *zIndex = userZIndices[stateNum];
216
- CGFloat elevation = (float )pow (2 .f , [zIndex floatValue ]);
217
- [_userElevations setObject: @(elevation) forKey: stateNum];
218
- }
219
- }
220
209
}
221
210
return self;
222
211
}
@@ -229,7 +218,7 @@ - (void)encodeWithCoder:(NSCoder *)aCoder {
229
218
}
230
219
231
220
[aCoder encodeBool: _shouldRaiseOnTouch forKey: MDCButtonShouldRaiseOnTouchKey];
232
- [aCoder encodeBool: _uppercaseTitle forKey: MDCButtonUppercaseTitleKey ];
221
+ [aCoder encodeBool: _shouldCapitalizeTitle forKey: MDCButtonShouldCapitalizeTitleKey ];
233
222
if (_underlyingColor) {
234
223
[aCoder encodeObject: _underlyingColor forKey: MDCButtonUnderlyingColorKey];
235
224
}
@@ -308,9 +297,9 @@ - (void)setEnabled:(BOOL)enabled animated:(BOOL)animated {
308
297
309
298
#pragma mark - Title Uppercasing
310
299
311
- - (void )setUppercaseTitle : (BOOL )uppercaseTitle {
312
- _uppercaseTitle = uppercaseTitle ;
313
- if (_uppercaseTitle ) {
300
+ - (void )setShouldCapitalizeTitle : (BOOL )shouldCapitalizeTitle {
301
+ _shouldCapitalizeTitle = shouldCapitalizeTitle ;
302
+ if (_shouldCapitalizeTitle ) {
314
303
// This ensures existing titles will get upper cased
315
304
UIControlState allControlStates =
316
305
(UIControlStateHighlighted | UIControlStateDisabled | UIControlStateSelected);
@@ -332,7 +321,7 @@ - (void)setTitle:(NSString *)title forState:(UIControlState)state {
332
321
[_accessibilityLabelForState removeObjectForKey: @(state)];
333
322
}
334
323
335
- if (_uppercaseTitle ) {
324
+ if (_shouldCapitalizeTitle ) {
336
325
title = [title uppercaseStringWithLocale: [NSLocale currentLocale ]];
337
326
}
338
327
[super setTitle: title forState: state];
@@ -347,7 +336,7 @@ - (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)
347
336
[_accessibilityLabelForState removeObjectForKey: @(state)];
348
337
}
349
338
350
- if (_uppercaseTitle ) {
339
+ if (_shouldCapitalizeTitle ) {
351
340
// Store the attributes.
352
341
NSMutableArray *attributes = [NSMutableArray array ];
353
342
[title enumerateAttributesInRange: NSMakeRange (0 , [title length ])
@@ -386,7 +375,7 @@ - (void)setAccessibilityLabel:(NSString *)accessibilityLabel {
386
375
}
387
376
388
377
- (NSString *)accessibilityLabel {
389
- if (!_uppercaseTitle ) {
378
+ if (!_shouldCapitalizeTitle ) {
390
379
return [super accessibilityLabel ];
391
380
}
392
381
@@ -463,6 +452,7 @@ - (void)setElevation:(CGFloat)elevation forState:(UIControlState)state {
463
452
464
453
// The elevation of the normal state controls whether this button is flat or not, and flat buttons
465
454
// have different background color requirements than raised buttons.
455
+ // TODO(ajsecord): Move to MDCFlatButton and update this comment.
466
456
if (state == UIControlStateNormal) {
467
457
[self updateAlphaAndBackgroundColorAnimated: NO ];
468
458
[self updateTitleColor ];
@@ -547,7 +537,16 @@ - (UIEdgeInsets)defaultContentEdgeInsets {
547
537
}
548
538
549
539
- (CGFloat)defaultElevationForState : (UIControlState)state {
550
- return 0 ;
540
+ if (state == UIControlStateNormal) {
541
+ return 0 ;
542
+ }
543
+
544
+ if ((state & UIControlStateSelected) == UIControlStateSelected) {
545
+ CGFloat normalElevation = [self elevationForState: UIControlStateNormal];
546
+ return normalElevation > 0 ? 2 * normalElevation : 1 ;
547
+ }
548
+
549
+ return [self elevationForState: UIControlStateNormal];
551
550
}
552
551
553
552
- (BOOL )shouldHaveOpaqueBackground {
0 commit comments