Buttons allow users to take actions, and make choices, with a single tap.
There are four types of buttons:
All Material buttons are implemented by MDCButton
, a subclass of UIButton.
MDCButton
MDCButton
is used to implement all four Material Buttons. In order to use MCDButton
, first add Buttons to your Podfile
:
pod MaterialComponents/Buttons
Then, run the installer.
pod install
After that, import the Buttons and initialize them using alloc
/init
.
import MaterialComponents.MaterialButtons import MaterialComponents.MaterialButtons_Theming let button = MDCButton()
#import "MaterialButtons.h" #import <MaterialComponents/MaterialButtons+Theming.h> MDCButton *button = [[MDCButton alloc] init];
To help ensure your buttons are accessible to as many users as possible, please be sure to review the following recommendations:
-accessibilityLabel
Set an appropriate accessibilityLabel
value if your button does not have a title. This is often the case with Floating Action Button instances which typically only have an icon.
button.accessibilityLabel = "Create"
button.accessibilityLabel = @"Create";
Make sure that your buttons have a minimum touch area. The Material spec for buttons calls for buttons that have a visual height of 36 and that touch areas should be at least 48 points high and 48 wide.
To keep a button's visual sizes small with larger touchable areas, set the hitAreaInsets
to a negative value. Be careful to maintain sufficient distance between the button touch targets. This will allow your button to have a large enough touch target while maintaining the desired visual appearance. For more see the Touch and click targets in the spec.
let buttonVerticalInset = min(0, -(kMinimumAccessibleButtonSize.height - button.bounds.height) / 2); let buttonHorizontalInset = min(0, -(kMinimumAccessibleButtonSize.width - button.bounds.width) / 2); button.hitAreaInsets = UIEdgeInsetsMake(buttonVerticalInset, buttonHorizontalInset, buttonVerticalInset, buttonHorizontalInset);
CGFloat verticalInset = MIN(0, -(48 - CGRectGetHeight(button.bounds)) / 2); CGFloat horizontalInset = MIN(0, -(48 - CGRectGetWidth(button.bounds)) / 2); button.hitAreaInsets = UIEdgeInsetsMake(verticalInset, horizontalInset, verticalInset, horizontalInset);
Set your buttons to have a minimum size. Material Buttons guidelines typically recommend a minimum height of 36 points and a minimum width of 64 points.
button.minimumSize = CGSize(width: 64, height: 48)
button.minimumSize = CGSizeMake(64, 36);
However there are some clear exceptions for these rules. Please adjust your buttons sizes accordingly.
accessibilityHint
Apple rarely recommends using the accessibilityHint
because the label should already be clear enough to indicate what will happen. Before you consider setting an -accessibilityHint
consider if you need it or if the rest of your UI could be adjusted to make it more contextually clear.
A well-crafted, thoughtful user interface can remove the need for accessibilityHint
in most situations. Examples for a selection dialog to choose one or more days of the week for a repeating calendar event:
accessibilityHint
values.accessibilityHint
value, “Toggles this day.”Text buttons are typically used for less-pronounced actions, including those located in dialogs and cards. In cards, text buttons help maintain an emphasis on card content.
To use a text button use the text button theming method on the MDCButton theming extension. For more information on theming extensions see the Theming section.
[self.button applyTextThemeWithScheme:self.containerScheme];
button.applyTextTheme(withScheme: containerScheme)
A text button has a text label, a transparent container and an optional icon.
Attribute | Related method(s) | Default value | |
---|---|---|---|
Text label | titleLabel | setTitle:forState: titleForState: | nil |
Color | titleLabel.textColor | setTitleColor:forState: titleColorForState: | Primary color |
Typography | titleLabel.font | setFont: and font on titleLabel | Button |
Attribute | Related method(s) | Default value | |
---|---|---|---|
Color | backgroundColor | setBackgroundColor:forState: backgroundColorForState | UIColor.clearColor |
Stroke color | setBorderColor:forState: borderColorForState: | nil | |
Stroke width | setBorderWidth:forState: borderWidthForState: | 0 | |
Ripple color | inkColor | setInkColor inkColor | Primary color at 12% opacity |
Attribute | Related method(s) | Default value | |
---|---|---|---|
Icon | imageView | setImage:forState: imageForState: | nil |
Color | imageView.tintColor | setImageViewTintColor:forState: imageViewTintColorForState: | nil |
Outlined buttons are medium-emphasis buttons. They contain actions that are important, but aren’t the primary action in an app.
To achieve an outlined button use the outlined button theming method on the MDCButton theming extension. To access the theming extension see the Theming section.
[self.button applyOutlinedThemeWithScheme:self.containerScheme];
button.applyOutlinedTheme(withScheme: containerScheme)
An outlined button has a text label, a container, and an optional icon.
Attribute | Related method(s) | Default value | |
---|---|---|---|
Text label | titleLabel | setTitle:forState: titleForState: | nil |
Color | titleLabel.textColor | setTitleColor:forState: titleColorForState: | Primary color |
Typography | titleLabel.font | setFont: and font on titleLabel | Button |
Attribute | Related method(s) | Default value | |
---|---|---|---|
Color | backgroundColor | setBackgroundColor:forState: backgroundColorForState | UIColor.clearColor |
Stroke color | setBorderColor:forState: borderColorForState: | On surface color at 12% opacity | |
Stroke width | setBorderWidth:forState: borderWidthForState: | 1 | |
Ripple color | inkColor | setInkColor inkColor | Primary color at 12% opacity |
Attribute | Related method(s) | Default value | |
---|---|---|---|
Icon | imageView | setImage:forState: imageForState: | nil |
Color | imageView.tintColor | setImageViewTintColor:forState: imageViewTintColorForState: | nil |
Contained buttons are high-emphasis, distinguished by their use of elevation and fill. They contain actions that are primary to your app.
Contained buttons are implemented by MDCButton. To achieve a contained button use the contained button theming method on the MDCButton theming extension. To access the theming extension see the Theming section.
[self.button applyContainedThemeWithScheme:self.containerScheme];
button.applyContainedTheme(withScheme: containerScheme)
A contained button has a text label, a container, and an optional icon.
Attribute | Related method(s) | Default value | |
---|---|---|---|
Text label | titleLabel | setTitle:forState: titleForState: | nil |
Color | titleLabel.textColor | setTitleColor:forState: titleColorForState: | On primary color |
Typography | titleLabel.font | setFont: and font on titleLabel | Button |
Attribute | Related method(s) | Default value | |
---|---|---|---|
Color | backgroundColor | setBackgroundColor:forState: backgroundColorForState | Primary color |
Stroke color | setBorderColor:forState: borderColorForState: | nil | |
Stroke width | setBorderWidth:forState: borderWidthForState: | nil | |
Ripple color | inkColor | setInkColor inkColor | On primary color at 12% opacity |
Attribute | Related method(s) | Default value | |
---|---|---|---|
Icon | imageView | setImage:forState: imageForState: | nil |
Color | imageView.tintColor | setImageViewTintColor:forState: imageViewTintColorForState: | nil |
Toggle buttons can be used to select from a group of choices. They are not supported on iOS.
You can theme an MDCButton to match any of the Material Button styles using theming extensions. Learn more about theming extensions. Below is a screenshot of Material Buttons with the Material Design Shrine theme:
To make use of the theming methods shown in the examples above install the Buttons theming extensions with Cocoapods. First, add the following line to your Podfile
.
pod MaterialComponents/Buttons+Theming
Then Run the installer.
pod install
Next, import the Buttons theming target and initialize a button.
import MaterialComponents.MaterialButtons import MaterialComponents.MaterialButtons_Theming let button = MDCButton()
#import "MaterialButtons.h" #import "MaterialButtons+Theming.h" MDCButton *button = [[MDCButton alloc] init];
From there, use the theming methods from the examples to achieve your preferred button style.