Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[ButtonBar] Implement new semantic color scheme themer APIs. (#3252)
Also updated the examples accordingly.

Pivotal story: https://www.pivotaltracker.com/story/show/156169509

![simulator screen shot - iphone 8 plus - 2018-04-05 at 12 57 34](https://user-images.githubusercontent.com/45670/38380177-f9ba1c46-38d0-11e8-9a6a-ef6f91a23d7e.png)
  • Loading branch information
jverkoey committed Apr 6, 2018
1 parent 6620da4 commit 5c09f5e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 32 deletions.
15 changes: 6 additions & 9 deletions components/ButtonBar/examples/ButtonBarTypicalUseExample.m
Expand Up @@ -17,6 +17,8 @@
#import <UIKit/UIKit.h>

#import "MaterialButtonBar.h"
#import "MaterialColorScheme.h"
#import "MDCButtonBarColorThemer.h"

@interface ButtonBarTypicalUseExample : UIViewController
@end
Expand All @@ -42,16 +44,11 @@ - (void)viewDidLoad {
target:self
action:@selector(didTapActionButton:)];

NSArray *items = @[ actionItem, secondActionItem ];
buttonBar.items = @[ actionItem, secondActionItem ];

// Set the title text attributes before assigning to buttonBar.items
// because of https://github.com/material-components/material-components-ios/issues/277
for (UIBarButtonItem *item in items) {
[item setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] }
forState:UIControlStateNormal];
}

buttonBar.items = items;
MDCSemanticColorScheme *scheme =
[[MDCSemanticColorScheme alloc] initWithDefaults:MDCColorSchemeDefaultsMaterial201804];
[MDCButtonBarColorThemer applySemanticColorScheme:scheme toButtonBar:buttonBar];

// MDCButtonBar's sizeThatFits gives a "best-fit" size of the provided items.
CGSize size = [buttonBar sizeThatFits:self.view.bounds.size];
Expand Down
26 changes: 4 additions & 22 deletions components/ButtonBar/examples/ButtonBarTypicalUseExample.swift
Expand Up @@ -23,7 +23,6 @@ class ButtonBarTypicalUseSwiftExample: UIViewController {
super.viewDidLoad()

let buttonBar = MDCButtonBar()
buttonBar.backgroundColor = self.buttonBarBackgroundColor()

// MDCButtonBar ignores the style of UIBarButtonItem.
let ignored: UIBarButtonItemStyle = .done
Expand All @@ -42,15 +41,10 @@ class ButtonBarTypicalUseSwiftExample: UIViewController {
action: #selector(didTapActionButton)
)

let items = [actionItem, secondActionItem]
buttonBar.items = [actionItem, secondActionItem]

// Set the title text attributes before assigning to buttonBar.items
// because of https://github.com/material-components/material-components-ios/issues/277
for item in items {
item.setTitleTextAttributes(self.itemTitleTextAttributes(), for: UIControlState())
}

buttonBar.items = items
let scheme = MDCSemanticColorScheme(defaults: .material201804)
MDCButtonBarColorThemer.applySemanticColorScheme(scheme, to: buttonBar)

// MDCButtonBar's sizeThatFits gives a "best-fit" size of the provided items.
let size = buttonBar.sizeThatFits(self.view.bounds.size)
Expand All @@ -62,7 +56,7 @@ class ButtonBarTypicalUseSwiftExample: UIViewController {
self.view.addSubview(buttonBar)

// Ensure that the controller's view isn't transparent.
view.backgroundColor = UIColor(white: 0.9, alpha:1.0)
view.backgroundColor = .white
}

@objc func didTapActionButton(_ sender: Any) {
Expand Down Expand Up @@ -96,15 +90,3 @@ extension ButtonBarTypicalUseSwiftExample {
}
}

// MARK: - Typical application code (not Material-specific)

extension ButtonBarTypicalUseSwiftExample {
func buttonBarBackgroundColor() -> UIColor {
return UIColor(white: 0.1, alpha: 1.0)
}

func itemTitleTextAttributes () -> [String: Any] {
let textColor = UIColor(white: 1, alpha: 0.8)
return [NSForegroundColorAttributeName: textColor]
}
}
16 changes: 15 additions & 1 deletion components/ButtonBar/src/ColorThemer/MDCButtonBarColorThemer.h
Expand Up @@ -14,8 +14,8 @@
limitations under the License.
*/

#import "MaterialThemes.h"
#import "MaterialButtonBar.h"
#import "MaterialColorScheme.h"

/**
Used to apply a color scheme to theme MDCButtonBar.
Expand All @@ -26,6 +26,20 @@
Applies a color scheme to theme a MDCButtonBar. Use a UIAppearance proxy to apply a color scheme to
all instances of MDCButtonBar.
@param colorScheme The color scheme to apply to MDCButtonBar.
@param buttonBar A MDCButtonBar instance to apply a color scheme.
*/
+ (void)applySemanticColorScheme:(nonnull id<MDCColorScheming>)colorScheme
toButtonBar:(nonnull MDCButtonBar *)buttonBar;

#pragma mark - Soon to be deprecated

/**
Applies a color scheme to theme a MDCButtonBar. Use a UIAppearance proxy to apply a color scheme to
all instances of MDCButtonBar.
This method will soon be deprecated. Consider using +applySemanticColorScheme:toButtonBar: instead.
@param colorScheme The color scheme to apply to MDCButtonBar.
@param buttonBar A MDCButtonBar instance to apply a color scheme.
*/
Expand Down
Expand Up @@ -18,6 +18,12 @@

@implementation MDCButtonBarColorThemer

+ (void)applySemanticColorScheme:(nonnull id<MDCColorScheming>)colorScheme
toButtonBar:(nonnull MDCButtonBar *)buttonBar {
buttonBar.backgroundColor = colorScheme.primaryColor;
buttonBar.tintColor = colorScheme.onPrimaryColor;
}

+ (void)applyColorScheme:(id<MDCColorScheme>)colorScheme
toButtonBar:(MDCButtonBar *)buttonBar {
buttonBar.backgroundColor = colorScheme.primaryColor;
Expand Down

0 comments on commit 5c09f5e

Please sign in to comment.