@@ -59,6 +59,32 @@ static inline BOOL MDCButtonFloatIsExactlyZero(CGFloat value) {
59
59
alpha: 1.0 ];
60
60
}
61
61
62
+ static NSAttributedString *capitalizeAttributedString (NSAttributedString *string) {
63
+ // Store the attributes.
64
+ NSMutableArray *attributes = [NSMutableArray array ];
65
+ [string enumerateAttributesInRange: NSMakeRange (0 , [string length ])
66
+ options: 0
67
+ usingBlock: ^(NSDictionary *attrs, NSRange range, BOOL *stop) {
68
+ [attributes addObject: @{
69
+ @" attrs" : attrs,
70
+ @" range" : [NSValue valueWithRange: range]
71
+ }];
72
+ }];
73
+
74
+ // Make the string uppercase.
75
+ NSString *uppercaseString = [[string string ] uppercaseStringWithLocale: [NSLocale currentLocale ]];
76
+
77
+ // Apply the text and attributes to a mutable copy of the title attributed string.
78
+ NSMutableAttributedString *mutableString = [string mutableCopy ];
79
+ [mutableString replaceCharactersInRange: NSMakeRange (0 , [string length ])
80
+ withString: uppercaseString];
81
+ for (NSDictionary *attribute in attributes) {
82
+ [mutableString setAttributes: attribute[@" attrs" ] range: [attribute[@" range" ] rangeValue ]];
83
+ }
84
+
85
+ return [mutableString copy ];
86
+ }
87
+
62
88
@interface MDCButton () {
63
89
NSMutableDictionary *_userElevations; // For each UIControlState.
64
90
NSMutableDictionary *_backgroundColors; // For each UIControlState.
@@ -293,13 +319,18 @@ - (void)setEnabled:(BOOL)enabled animated:(BOOL)animated {
293
319
- (void )setShouldCapitalizeTitle : (BOOL )shouldCapitalizeTitle {
294
320
_shouldCapitalizeTitle = shouldCapitalizeTitle;
295
321
if (_shouldCapitalizeTitle) {
296
- // This ensures existing titles will get upper cased
297
- UIControlState allControlStates =
298
- (UIControlStateHighlighted | UIControlStateDisabled | UIControlStateSelected) ;
299
- for (UIControlState state = 0 ; state < allControlStates; ++state) {
322
+ // This ensures existing titles will get capitalized.
323
+ UIControlState allControlStates = UIControlStateNormal | UIControlStateHighlighted |
324
+ UIControlStateDisabled | UIControlStateSelected;
325
+ for (UIControlState state = 0 ; state <= allControlStates; ++state) {
300
326
NSString *title = [self titleForState: state];
301
327
if (title) {
302
- [self setTitle: title forState: state];
328
+ [self setTitle: [title uppercaseStringWithLocale: [NSLocale currentLocale ]] forState: state];
329
+ }
330
+
331
+ NSAttributedString *attributedTitle = [self attributedTitleForState: state];
332
+ if (attributedTitle) {
333
+ [self setAttributedTitle: capitalizeAttributedString (attributedTitle) forState: state];
303
334
}
304
335
}
305
336
}
@@ -330,29 +361,7 @@ - (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)
330
361
}
331
362
332
363
if (_shouldCapitalizeTitle) {
333
- // Store the attributes.
334
- NSMutableArray *attributes = [NSMutableArray array ];
335
- [title enumerateAttributesInRange: NSMakeRange (0 , [title length ])
336
- options: 0
337
- usingBlock: ^(NSDictionary *attrs, NSRange range, BOOL *stop) {
338
- [attributes addObject: @{
339
- @" attrs" : attrs,
340
- @" range" : [NSValue valueWithRange: range]
341
- }];
342
- }];
343
-
344
- // Make the title text uppercase.
345
- NSString *uppercaseTitle = [[title string ] uppercaseStringWithLocale: [NSLocale currentLocale ]];
346
-
347
- // Apply the text and attributes to a mutable copy of the title attributed string.
348
- NSMutableAttributedString *mutableTitle = [title mutableCopy ];
349
- [mutableTitle replaceCharactersInRange: NSMakeRange (0 , [title length ])
350
- withString: uppercaseTitle];
351
- for (NSDictionary *attribute in attributes) {
352
- [mutableTitle setAttributes: attribute[@" attrs" ] range: [attribute[@" range" ] rangeValue ]];
353
- }
354
-
355
- title = [mutableTitle copy ];
364
+ title = capitalizeAttributedString (title);
356
365
}
357
366
[super setAttributedTitle: title forState: state];
358
367
}
0 commit comments