material-components_materia.../components/TextFields/examples/TextFieldControllerStylesExample.m
Will Larche bc119b3b73 [TextFields] Example cleanup. (#4392)
* [TextFields] Example cleanup.

* [TextFields] Copy correction

* [TextFields] Example file rename.
2018-06-12 17:41:02 -04:00

256 lines
11 KiB
Objective-C

/*
Copyright 2016-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 "MaterialTextFields.h"
#import "supplemental/TextFieldControllerStylesExampleSupplemental.h"
@interface TextFieldControllerStylesExample ()
// Be sure to keep your controllers in memory somewhere like a property:
@property(nonatomic, strong) MDCTextInputControllerOutlined *textFieldControllerOutlined;
@property(nonatomic, strong) MDCTextInputControllerFilled *textFieldControllerFilled;
@property(nonatomic, strong) MDCTextInputControllerUnderline *textFieldControllerUnderline;
@end
@implementation TextFieldControllerStylesExample
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithWhite:0.97f alpha:1.0f];
self.title = @"Material Text Fields";
[self setupExampleViews];
[self setupTextFields];
[self registerKeyboardNotifications];
}
- (void)setupTextFields {
// Default with Character Count and Floating Placeholder Text Fields
// First the text field is added to the view hierarchy
MDCTextField *textFieldOutlined = [[MDCTextField alloc] init];
[self.scrollView addSubview:textFieldOutlined];
textFieldOutlined.translatesAutoresizingMaskIntoConstraints = NO;
int characterCountMax = 25;
textFieldOutlined.delegate = self;
textFieldOutlined.clearButtonMode = UITextFieldViewModeAlways;
// Second the controller is created to manage the text field
self.textFieldControllerOutlined =
[[MDCTextInputControllerOutlined alloc] initWithTextInput:textFieldOutlined];
self.textFieldControllerOutlined.placeholderText =
@"MDCTextInputControllerOutlined";
self.textFieldControllerOutlined.characterCountMax = characterCountMax;
[self.textFieldControllerOutlined mdc_setAdjustsFontForContentSizeCategory:YES];
MDCTextField *textFieldFilled = [[MDCTextField alloc] init];
[self.scrollView addSubview:textFieldFilled];
textFieldFilled.translatesAutoresizingMaskIntoConstraints = NO;
textFieldFilled.delegate = self;
textFieldFilled.clearButtonMode = UITextFieldViewModeUnlessEditing;
self.textFieldControllerFilled =
[[MDCTextInputControllerFilled alloc] initWithTextInput:textFieldFilled];
self.textFieldControllerFilled.placeholderText = @"MDCTextInputControllerFilled";
self.textFieldControllerFilled.characterCountMax = characterCountMax;
[self.textFieldControllerFilled mdc_setAdjustsFontForContentSizeCategory:YES];
[NSLayoutConstraint
activateConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:[charMax]-[floating]"
options:NSLayoutFormatAlignAllLeading |
NSLayoutFormatAlignAllTrailing
metrics:nil
views:@{
@"charMax" : textFieldOutlined,
@"floating" : textFieldFilled
}]];
[NSLayoutConstraint constraintWithItem:textFieldOutlined
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeadingMargin
multiplier:1
constant:0]
.active = YES;
[NSLayoutConstraint constraintWithItem:textFieldOutlined
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailingMargin
multiplier:1
constant:0]
.active = YES;
[NSLayoutConstraint constraintWithItem:textFieldOutlined
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeTrailingMargin
multiplier:1
constant:0]
.active = YES;
// Full Width Text Field
MDCTextField *textFieldUnderline = [[MDCTextField alloc] init];
[self.scrollView addSubview:textFieldUnderline];
textFieldUnderline.translatesAutoresizingMaskIntoConstraints = NO;
textFieldUnderline.delegate = self;
textFieldUnderline.clearButtonMode = UITextFieldViewModeUnlessEditing;
self.textFieldControllerUnderline =
[[MDCTextInputControllerUnderline alloc] initWithTextInput:textFieldUnderline];
self.textFieldControllerUnderline.placeholderText = @"MDCTextInputControllerUnderline";
self.textFieldControllerUnderline.characterCountMax = characterCountMax;
[self.textFieldControllerUnderline mdc_setAdjustsFontForContentSizeCategory:YES];
[NSLayoutConstraint constraintWithItem:textFieldUnderline
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:textFieldFilled
attribute:NSLayoutAttributeBottom
multiplier:1
constant:1]
.active = YES;
[NSLayoutConstraint constraintWithItem:textFieldUnderline
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeadingMargin
multiplier:1
constant:0]
.active = YES;
[NSLayoutConstraint constraintWithItem:textFieldUnderline
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailingMargin
multiplier:1
constant:0]
.active = YES;
#if defined(__IPHONE_11_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_11_0)
if (@available(iOS 11.0, *)) {
[NSLayoutConstraint constraintWithItem:textFieldOutlined
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView.contentLayoutGuide
attribute:NSLayoutAttributeTop
multiplier:1
constant:20]
.active = YES;
[NSLayoutConstraint constraintWithItem:textFieldOutlined
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView.contentLayoutGuide
attribute:NSLayoutAttributeBottom
multiplier:1
constant:-20]
.active = YES;
} else {
[NSLayoutConstraint constraintWithItem:textFieldOutlined
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeTop
multiplier:1
constant:20]
.active = YES;
[NSLayoutConstraint constraintWithItem:textFieldOutlined
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeBottom
multiplier:1
constant:-20]
.active = YES;
}
#else
[NSLayoutConstraint constraintWithItem:textFieldOutlined
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeTop
multiplier:1
constant:20]
.active = YES;
[NSLayoutConstraint constraintWithItem:textFieldOutlined
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationLessThanOrEqual
toItem:self.scrollView
attribute:NSLayoutAttributeTop
multiplier:1
constant:-20]
.active = YES;
#endif
}
#pragma mark - UITextFieldDelegate
// All the usual UITextFieldDelegate methods work with MDCTextField
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
#pragma mark - Keyboard Handling
- (void)registerKeyboardNotifications {
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[defaultCenter addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardDidChangeFrameNotification
object:nil];
[defaultCenter addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification {
CGRect frame = [[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, CGRectGetHeight(frame), 0);
}
- (void)keyboardWillHide:(NSNotification *)notification {
self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}
@end