Skip to content

Commit

Permalink
[ButtonBar] Add uppercasesButtonTitles API for modifying title casing…
Browse files Browse the repository at this point in the history
… behavior. (#4935)

This change enables us to expose a similar API in MDCNavigationBar that will enable configuration of title casing behaviors for navigation bar button bars.

Step 1/2 for https://github.com/material-components/material-components-ios/issues/2968
  • Loading branch information
jverkoey committed Aug 29, 2018
1 parent 42d0c8d commit 5d0e7cc
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
9 changes: 9 additions & 0 deletions components/ButtonBar/src/MDCButtonBar.h
Expand Up @@ -104,6 +104,15 @@ IB_DESIGNABLE
*/
@property(nonatomic) CGFloat buttonTitleBaseline;

/**
If true, all button titles will be converted to uppercase.
Changing this property to NO will update the current title string for all buttons.
Default is YES.
*/
@property(nonatomic) BOOL uppercasesButtonTitles;

/**
Sets the title font for the given state for all buttons.
Expand Down
13 changes: 13 additions & 0 deletions components/ButtonBar/src/MDCButtonBar.m
Expand Up @@ -43,6 +43,7 @@ - (void)dealloc {
}

- (void)commonMDCButtonBarInit {
_uppercasesButtonTitles = YES;
_buttonItemsLock = [[NSObject alloc] init];
_layoutPosition = MDCButtonBarLayoutPositionNone;

Expand Down Expand Up @@ -403,6 +404,18 @@ - (void)setItems:(NSArray<UIBarButtonItem *> *)items {
}
}

- (void)setUppercasesButtonTitles:(BOOL)uppercasesButtonTitles {
_uppercasesButtonTitles = uppercasesButtonTitles;

for (NSUInteger i = 0; i < [_buttonViews count]; ++i) {
UIView *viewObj = _buttonViews[i];
if ([viewObj isKindOfClass:[MDCButton class]]) {
MDCButton *button = (MDCButton *)viewObj;
button.uppercaseTitle = uppercasesButtonTitles;
}
}
}

- (void)setButtonsTitleFont:(UIFont *)font forState:(UIControlState)state {
[_defaultBuilder setTitleFont:font forState:state];

Expand Down
Expand Up @@ -129,6 +129,7 @@ - (UIView *)buttonBar:(MDCButtonBar *)buttonBar

[MDCAppBarButtonBarBuilder configureButton:button fromButtonItem:buttonItem];

button.uppercaseTitle = buttonBar.uppercasesButtonTitles;
[button setTitleColor:self.buttonTitleColor forState:UIControlStateNormal];
[button setUnderlyingColorHint:self.buttonUnderlyingColor];
for (NSNumber *state in _fonts) {
Expand Down
@@ -0,0 +1,81 @@
/*
Copyright 2018-present the Material Components for iOS authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import XCTest
import MaterialComponents.MaterialButtonBar
import MaterialComponents.MaterialButtons

class ButtonBarButtonTitleCasingTests: XCTestCase {

var buttonBar: MDCButtonBar!

override func setUp() {
buttonBar = MDCButtonBar()
}

func testDefaultCasingIsUppercase() {
// Then
XCTAssertTrue(buttonBar.uppercasesButtonTitles)
}

func testButtonTitlesAreUppercasedByDefault() {
// Given
let items = [UIBarButtonItem(title: "Text", style: .plain, target: nil, action: nil)]

// When
buttonBar.items = items

// Then
XCTAssertTrue(buttonBar.uppercasesButtonTitles)
for view in buttonBar.subviews {
if let button = view as? MDCButton {
XCTAssertEqual(button.title(for: .normal), "TEXT")
}
}
}

func testButtonTitlesAreNotUppercasedWhenFlagIsDisabledBeforeAssignment() {
// Given
let items = [UIBarButtonItem(title: "Text", style: .plain, target: nil, action: nil)]

// When
buttonBar.uppercasesButtonTitles = false
buttonBar.items = items

// Then
for view in buttonBar.subviews {
if let button = view as? MDCButton {
XCTAssertEqual(button.title(for: .normal), "Text")
}
}
}

func testButtonTitlesAreNotUppercasedWhenFlagIsDisabledAfterAssignment() {
// Given
let items = [UIBarButtonItem(title: "Text", style: .plain, target: nil, action: nil)]
buttonBar.items = items

// When
buttonBar.uppercasesButtonTitles = false

// Then
for view in buttonBar.subviews {
if let button = view as? MDCButton {
XCTAssertEqual(button.title(for: .normal), "Text")
}
}
}
}

0 comments on commit 5d0e7cc

Please sign in to comment.