-
Notifications
You must be signed in to change notification settings - Fork 944
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TextControls] Add typical use example
PiperOrigin-RevId: 323996387
- Loading branch information
1 parent
f75ff1a
commit 9b6db6f
Showing
2 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
components/TextControls/examples/supplemental/MDCTextControlTextFieldTypicalUseExample.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2019-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 <UIKit/UIKit.h> | ||
|
||
#import "MaterialContainerScheme.h" | ||
|
||
@interface MDCTextControlTextFieldTypicalUseExample : UIViewController | ||
|
||
/** | ||
The MDCContainerScheming for the class. | ||
*/ | ||
@property(strong, nonatomic) id<MDCContainerScheming> containerScheme; | ||
|
||
@end |
117 changes: 117 additions & 0 deletions
117
components/TextControls/examples/supplemental/MDCTextControlTextFieldTypicalUseExample.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright 2019-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 <UIKit/UIKit.h> | ||
|
||
#import "MDCTextControlTextFieldTypicalUseExample.h" | ||
|
||
#import "MaterialButtons.h" | ||
#import "MaterialButtons+Theming.h" | ||
#import "MaterialTextControls+FilledTextFields.h" | ||
#import "MaterialTextControls+FilledTextFieldsTheming.h" | ||
#import "MaterialColorScheme.h" | ||
#import "MaterialContainerScheme.h" | ||
|
||
static NSString *const kExampleTitle = @"Text fields (typical use)"; | ||
static CGFloat const kPadding = 20; | ||
|
||
@interface MDCTextControlTextFieldTypicalUseExample () | ||
@property(strong, nonatomic) MDCFilledTextField *textField; | ||
@property(strong, nonatomic) MDCButton *toggleErrorButton; | ||
@property(nonatomic, assign) BOOL shouldApplyErrorTheming; | ||
@end | ||
|
||
@implementation MDCTextControlTextFieldTypicalUseExample | ||
|
||
#pragma mark View Controller Lifecycle | ||
|
||
- (void)viewDidLoad { | ||
[super viewDidLoad]; | ||
|
||
self.view.backgroundColor = self.containerScheme.colorScheme.backgroundColor; | ||
self.title = kExampleTitle; | ||
|
||
self.toggleErrorButton = [self createToggleErrorButton]; | ||
[self.view addSubview:self.toggleErrorButton]; | ||
|
||
[self.view.leftAnchor constraintEqualToAnchor:self.toggleErrorButton.leftAnchor | ||
constant:-kPadding] | ||
.active = YES; | ||
[self.topLayoutGuide.bottomAnchor constraintEqualToAnchor:self.toggleErrorButton.topAnchor | ||
constant:-kPadding] | ||
.active = YES; | ||
|
||
self.textField = [self createFilledTextField]; | ||
[self.view addSubview:self.textField]; | ||
|
||
[self.view.leftAnchor constraintEqualToAnchor:self.textField.leftAnchor constant:-kPadding] | ||
.active = YES; | ||
[self.view.rightAnchor constraintEqualToAnchor:self.textField.rightAnchor constant:kPadding] | ||
.active = YES; | ||
[self.toggleErrorButton.bottomAnchor constraintEqualToAnchor:self.textField.topAnchor | ||
constant:-kPadding] | ||
.active = YES; | ||
} | ||
|
||
- (void)toggleErrorButtonTapped:(UIButton *)button { | ||
[self handleToggleErrorButtonTapped]; | ||
} | ||
|
||
- (void)handleToggleErrorButtonTapped { | ||
self.shouldApplyErrorTheming = !self.shouldApplyErrorTheming; | ||
if (self.shouldApplyErrorTheming) { | ||
[self.textField applyErrorThemeWithScheme:self.containerScheme]; | ||
} else { | ||
[self.textField applyThemeWithScheme:self.containerScheme]; | ||
} | ||
} | ||
|
||
- (MDCFilledTextField *)createFilledTextField { | ||
MDCFilledTextField *textField = [[MDCFilledTextField alloc] init]; | ||
textField.translatesAutoresizingMaskIntoConstraints = NO; | ||
textField.adjustsFontForContentSizeCategory = YES; | ||
textField.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody]; | ||
textField.label.text = @"Label text"; | ||
[textField applyThemeWithScheme:self.containerScheme]; | ||
return textField; | ||
} | ||
|
||
- (MDCButton *)createToggleErrorButton { | ||
MDCButton *button = [[MDCButton alloc] init]; | ||
button.translatesAutoresizingMaskIntoConstraints = NO; | ||
button.mdc_adjustsFontForContentSizeCategory = YES; | ||
[button setTitle:@"Toggle error" forState:UIControlStateNormal]; | ||
[button addTarget:self | ||
action:@selector(toggleErrorButtonTapped:) | ||
forControlEvents:UIControlEventTouchUpInside]; | ||
[button applyContainedThemeWithScheme:self.containerScheme]; | ||
[button sizeToFit]; | ||
return button; | ||
} | ||
|
||
@end | ||
|
||
#pragma mark - CatalogByConvention | ||
|
||
@implementation MDCTextControlTextFieldTypicalUseExample (CatalogByConvention) | ||
|
||
+ (NSDictionary *)catalogMetadata { | ||
return @{ | ||
@"breadcrumbs" : @[ @"Text Controls", kExampleTitle ], | ||
@"primaryDemo" : @YES, | ||
@"presentable" : @YES, | ||
}; | ||
} | ||
|
||
@end |