Skip to content

Commit fabf5af

Browse files
committed
Fixed attributed title capitalization.
1 parent 322eb6c commit fabf5af

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

components/Buttons/src/MDCButton.m

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,32 @@ static inline BOOL MDCButtonFloatIsExactlyZero(CGFloat value) {
5959
alpha:1.0];
6060
}
6161

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+
6288
@interface MDCButton () {
6389
NSMutableDictionary *_userElevations; // For each UIControlState.
6490
NSMutableDictionary *_backgroundColors; // For each UIControlState.
@@ -293,13 +319,18 @@ - (void)setEnabled:(BOOL)enabled animated:(BOOL)animated {
293319
- (void)setShouldCapitalizeTitle:(BOOL)shouldCapitalizeTitle {
294320
_shouldCapitalizeTitle = shouldCapitalizeTitle;
295321
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) {
300326
NSString *title = [self titleForState:state];
301327
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];
303334
}
304335
}
305336
}
@@ -330,29 +361,7 @@ - (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)
330361
}
331362

332363
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);
356365
}
357366
[super setAttributedTitle:title forState:state];
358367
}

0 commit comments

Comments
 (0)