material-components_materia.../components/TextFields/examples/TextFieldControllerStylesExample.m
Will Larche 1f10ae3e3e
[TextFields] Making leading and trailing view layout customizeable (#4434)
Closes the layout part of b/74174981

***Breaks screenshot tests.

Before:
![simulator screen shot - iphone x - 2018-06-24 at 17 39 27](https://user-images.githubusercontent.com/1271525/41823956-2742affe-77d6-11e8-9082-2ccb10071750.png)

After:
![simulator screen shot - iphone x - 2018-06-24 at 17 35 45](https://user-images.githubusercontent.com/1271525/41823875-13f44152-77d5-11e8-8ae8-f23840efe6e1.png)

Also corrects the x of the floating placeholder on a filled text field.
![simulator screen shot - iphone x - 2018-06-24 at 17 45 43](https://user-images.githubusercontent.com/1271525/41823974-7e8c5346-77d6-11e8-88ce-38f4adb32323.png)
2018-06-25 07:59:33 -07:00

287 lines
12 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;
@property(nonatomic, strong) UIImage *leadingImage;
@property(nonatomic, strong) UIImage *trailingImage;
@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 setupImages];
[self setupTextFields];
[self registerKeyboardNotifications];
}
- (void)setupImages {
self.leadingImage = [UIImage imageNamed:@"ic_search"
inBundle:[NSBundle bundleForClass:[TextFieldControllerStylesExample
class]]
compatibleWithTraitCollection:nil];
self.trailingImage =
[UIImage imageNamed:@"ic_done"
inBundle:[NSBundle
bundleForClass:[TextFieldControllerStylesExample class]]
compatibleWithTraitCollection:nil];
}
- (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;
textFieldOutlined.leadingView = [[UIImageView alloc] initWithImage:self.leadingImage];
textFieldOutlined.leadingViewMode = UITextFieldViewModeAlways;
textFieldOutlined.trailingView = [[UIImageView alloc] initWithImage:self.trailingImage];
textFieldOutlined.trailingViewMode = 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;
textFieldFilled.leadingView = [[UIImageView alloc] initWithImage:self.leadingImage];
textFieldFilled.leadingViewMode = UITextFieldViewModeAlways;
textFieldFilled.trailingView = [[UIImageView alloc] initWithImage:self.trailingImage];
textFieldFilled.trailingViewMode = UITextFieldViewModeAlways;
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;
textFieldUnderline.leadingView = [[UIImageView alloc] initWithImage:self.leadingImage];
textFieldUnderline.leadingViewMode = UITextFieldViewModeAlways;
textFieldUnderline.trailingView = [[UIImageView alloc] initWithImage:self.trailingImage];
textFieldUnderline.trailingViewMode = UITextFieldViewModeAlways;
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