mirror of
https://github.com/material-components/material-components-ios.git
synced 2026-02-20 08:27:32 +08:00
* [TextFiels] Left View. * [TextFields] Tile art. * [TextFields] Left view works with floating. * [Catalog] Correcting extension identifier. * [TextFields] Formatting and corrections because of static analyzer. * [TextFields] Fixes unit tests for change in property name. * [TextFields] Precision warning correction. * [TextFields] Examples for .rightView * [TextFields] Left / right existence tests. * [TextFields] Truncation for rightView and leftView. * [TextFields] Placeholder positioning clean up. * [TextFields] Full width padding. * [TextFields] Initializer clean up. * [TextFields] Bounds layout correction. * [TextFields] Cleanup. * [TextFields] Organizing constraints. * [TextFields] Conforming better to auto layout lifecycle. * [TextFields] Cleaning up colors. * [TextFields] Correction to auto layout problems with textViews. * [TextFields] NSCoder and NSCopying support begun. * [TextFields] NSCoding and NSCopying support. API documentation and cleanup. Internal alphabetizing. * [TextFields] Spacing correction. * [TextFields] TextView test correction. * [TextFields] Dynamic Type support. * [TextFields] Dynamic Type in example. * [TextFields] Correction for broken left view. * [TextFields] Beginning readme. * [TextFields] Readme work. * [TextFields] Markdown correction. * [TextFields] Markdown correction. * [TextFields] Objective C example and renaming to plural. * [TextFields] First snippet. * [TextFields] Updated readme with snippets. * [TextFields] Objc example. * [TextFields] Swift example cleanup. * [TextFields] Added text view snippet. * [TextFields] Readme clarification. * [TextFields] Expanding positioning delegate scope. * [TextFields] Making text container inset the default API for controlling positioning. * [TextFields] Formatting. * [TextField] Moving underline to auto layout. * [TextFields] Underline content hugging. * [TextFields] Added unstyled text view to the examples. * [TextField] Added notification observation for text views. * [TextFields] Vastly improved spacing. * [TextFields] Improving layout. * [TextFields] Height correction. * [TextFields] Improvements to height calculations. * [TextFields] Preparations for .leftView and .rightView calculations. * [TextFields] Moving line height calculations to rounded instead of veiled. * [TextFields] Unstyled textfield done. * [TextFields] Full width char count layout correction. * [TextFields] Example correction. * [TextFields] Removing TODOs. * [TextFields] Getting rid of todos (colors.) * [TextFields] Left and right view position correction. * [TextFields] Editing rect corrections. * [TextFields] Associate view alignment * [TextFields] Removing duplicated work. * [TextFields] Cleaning up positioning delegate. * [TextFields] Improving layout. * [TextFields] Editing for clarity. * [TextField] Precision correction. * [TextFields] Height corrections for .floatingPlaceholder * [TextFields] Improved .floatingPlaceholder height math * [TextFields] Removing Debugging consoles. * [TextFields] Custom font improved support and “remembering” in controller. * [TextFields] Cleaning up KVO and adjusting for bigger fonts. * [TextFields] TextView NSCoding correction. * [TextFields] Format all. * [TextFields] Added comments. * [TextFields] Moved code to match pattern of organization. * [TextFields] Properties organization. * [TextFields] Organization. * [TextFields] Dealing with notifications and setting textInput. * [TextFields] Reduced duplicated math. Moved to text insets as source of truth. * [TextFields] Comments. * [TextFields] Formatting. * [TextFields] Comments correction. * [TextFields] Full width spacing for textRect. * [TextFields] Full width spacing for editingRect. * [TextFields] Necessary for KVO. * [TextFields] Clear button Y correction. * [TextFields] Dealing with keyboard in example. * [TextFields] whitespace correction. * [TextField] Text views examples simple. * [TextFields] TextView hugging increase. * [TextFields] Improved examples. * [TextFields] Improving examples for text views. * [TextFieds] text views example improvement * [TextFields] Temporarily removed text views and expanded example for text fields. * [TextFields] Objc example work. * [TextFields] Comment improvement. * [TextFields] Expanding Objc Example. * [TextFields] Shaving tests for single line only. * [TextFields] Corrected character count not coloring underline. * [TextFields] Character count examples and a fix. * [TextFields] Text field underline space correction. * [TextFields] Removing debug code. * [TextFields] Character count logic correction. * [TextFields] Beginning RTL calculations for textRects * [TextFields] More RTL spacing work. * [TextFields] Example improvement. * [TextFields] Correction for clear button. * [TextFields] Improved RTL. * [TextFields] Storyboards example and fix for preset placeholder. * [TextFields] IB example cleanup. * [TextFields] IB example improvement. * [TextField] Swift example has controls at the bottom * [TextField] Improved voiceover support. * [TextFields] Improved voice over support. * [TextFields] Formatting. * [TextFields] Improving voice over support. * [TextFields] VoiceOver support for errors. * [TextFields] VoiceOver detail. * [TextFields] Formatting and details. * [TextFields] Better storyboard support. * Design * [TextFields] Podfile.lock * [Typography] Undoing formatting. * [FeatureHighlight] Undoing formatting. * [TextFields] Undoing file change. * [TextFields] Adding missing EOL whitespace. * [FeatureHightlight] Formatting. * [Typography] Formatting correction. * [TextFields] Feedback from PR. * [TextFields] Removing code because PR. * [TextFields] Renaming because PR. * [TextFields] Replaced theme with compliant. * [TextFields] Changes per PR. * [TextFields] Renaming things for clarity. * [TextFields] Removing unused protocols. * [TextFields] Clear button size logic. * [TextFields] Some corrections dealing with serialization. * [TextFields] Example correction and testing code. * [TextFields] Support for iOS 10’s adjustsFontForContentSizeCategory * [TextFields] Removing debug comments. * [TextFields] Better handling of custom fonts and dynamic type. * [TextFields] Better organization of new methods. * [TextFields] Removing unneeded code. * [TextFields] Improved floating placeholder calculations. * [TextFields] New clear button implementation. * [TextFields] Clear button details. * [TextFields] Formatting. * [TextFields] A little safety. * [TextFields] Clear Button target action. * [TextFields] Cleanup. * [TextFields] CGFloat conversion. * [TextFields] Removing unused method. * [TextFields] Minor corrections to clear button. * [TextFields] Added MDCMath. * [TextFields] NSNotificationCenter cleanup. * [TextFields] Cleaning up logic. * [TextFields] Clear button bug fix. * [TextFields] Formatting. * [TextFields] Dynamic type correction. * [TextFields] Improvement to example. * [TextFields] Placeholder truncation bug fix. * [TextFields] RTL improvements. * [TextFields] Examples reorganized. * [TextFields] Better typical use example (form). * [TextFields] Better support for changing presentation styles while editing. * [Catalog] Reverting changes to project and info.plist. * [TextFields] Style correction. * [TextFields] New example has validation. * [TextFields] Correcting property declaration. * [TextFields] Simplifying iOS version check. * [TextField] Adding faux-value semantics to controller properties. * [TextFields] Added MDCMath. * [TextFields] Removing development team. * [TextFields] Removing debug identifier. * [TextFields] Reverting debug change. * [TextFields] Kitchen sink automatically scrolls with keyboard now. * [TextFields] Main example doesn’t try to show full width. * [Pods] Podfile.lock. * [TextFields] Example improvements. * [TextFields] Organization in example. * [TextField] Catalog tile to BnW. * [TextFields] Cleanup. * [TextFields] Screenshot * [Tabs] README correction. * [TextFields] Example correction. * [TextFields] - Removed video link in readme * [TextFields] - Updated image width * [TextFields] Example cleanup. * [TextFields] Manual layout example. * [TextFields] Finishing the renaming of the fundament. * [TextField] Correcting typo. * [TextFields] Adding underline as a UIView in API. * [TextFields] Correcting constraints for floating / default. * [TextFields] Manual example support for changing presentation improvement. * [TextFields] Manual layout support. * [TextFields] Correcting copied properties. * [TextFields] Comments improvement. * [TextFields] More validation examples. * [TextFields] Naming update. * [TextFields] More tests and corrections of problems discovered during testing. * [TextFields] Clear button mode in copy. * [TextField] Attributed setters tests. * [TextFields] Color test correction. * [TextField] ErrorColor test. * [TextFields] Formatting. * [TextFields] Error color fixes. * [TextFields] More tests. * [TextFields] Testing correction. * [TextFields] Organizing test files. * [TextField] Test organization. * [TextField] Additional test. * [FeatureHighlight] Reverting change. * [TextFields] Bare pod file update. * [TextField] Test corrections. * [TextField] Color has value semantic. * [TextField] Documentation updates. * [TextFields] Documentation improvement. * [TextField] RTL corrections. * [TextField] RTL and layout corrections. * [TextFields] Better AutoLayout use. * [TextFields] Removing debugging logging. * [TextFields] Exposing underline fully. * [TextFields] KVO for new underline API. * [TextFields] Formatting * [TextFields] Documentation update. * [TextFields] Comments correction. * [TextFields] Updating README. * [TextFields] Standardizing primitive property annotations. * [TextFields] PR notes. * [TextFields] Localization todos. * [TextFields] Const definition corrections. * [TextFields] Correcting const definitions. * [TextFields] Using MDCMath. * [TextFields] Correcting const definition. * [TextFields] Color value semantics correction. * [TextFields] Copy update. * [TextFields] Removing unnecessary code. * [TextFields] Copy update. * [TextFields] Underline inherited code correction. * [TextFields] Switch fix. * [TextFields] Simplifying some math. * [TextFields] Simplyfing some logic. * [TextFields] Improving underline init order. * [TextField] Undoing formatting changes. * [TextFields] Adjusting to new Swift color API. * [TextFields] Re-adding textFields catalog tile. * [TextField] Refining Swift support choices. * [TextField] Simplifying swift support. * [TextFields] Improving Swift support. * [TextFields] Helper text implementation improvement. * [TextField] A little CAAnimation safety. * [TextFields] Removing extra line. * [TextFields] CATransaction a little better.
246 lines
11 KiB
Objective-C
246 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 "TextFieldPresentationStylesExampleSupplemental.h"
|
|
|
|
@import MaterialComponents.MaterialTextFields;
|
|
|
|
@interface TextFieldPresentationStylesExample ()
|
|
|
|
// Be sure to keep your controllers in memory somewhere like a property:
|
|
@property(nonatomic, strong) MDCTextInputController *textFieldControllerDefaultCharMax;
|
|
@property(nonatomic, strong) MDCTextInputController *textFieldControllerFloating;
|
|
@property(nonatomic, strong) MDCTextInputController *textFieldControllerFullWidth;
|
|
|
|
@end
|
|
|
|
@implementation TextFieldPresentationStylesExample
|
|
|
|
- (void)viewDidLoad {
|
|
[super viewDidLoad];
|
|
|
|
self.view.backgroundColor = [UIColor colorWithWhite:0.97 alpha:1.0];
|
|
self.title = @"Material Text Fields";
|
|
|
|
[self setupExampleViews];
|
|
|
|
[self setupTextFields];
|
|
}
|
|
|
|
- (void)setupTextFields {
|
|
// Default with Character Count and Floating Placeholder Text Fields
|
|
|
|
// First the text field is added to the view hierarchy
|
|
MDCTextField *textFieldDefaultCharMax = [[MDCTextField alloc] init];
|
|
[self.scrollView addSubview:textFieldDefaultCharMax];
|
|
textFieldDefaultCharMax.translatesAutoresizingMaskIntoConstraints = NO;
|
|
|
|
int defaultMax = 25;
|
|
textFieldDefaultCharMax.placeholder =
|
|
[NSString stringWithFormat:@"Enter up to %d characters", defaultMax];
|
|
textFieldDefaultCharMax.delegate = self;
|
|
textFieldDefaultCharMax.clearButtonMode = UITextFieldViewModeAlways;
|
|
|
|
// Second the controller is created to manage the text field
|
|
self.textFieldControllerDefaultCharMax =
|
|
[[MDCTextInputController alloc] initWithTextInput:textFieldDefaultCharMax];
|
|
self.textFieldControllerDefaultCharMax.characterCountMax = defaultMax;
|
|
[self.textFieldControllerDefaultCharMax mdc_setAdjustsFontForContentSizeCategory:YES];
|
|
|
|
MDCTextField *textFieldFloating = [[MDCTextField alloc] init];
|
|
[self.scrollView addSubview:textFieldFloating];
|
|
textFieldFloating.translatesAutoresizingMaskIntoConstraints = NO;
|
|
|
|
textFieldFloating.placeholder = @"Floating Placeholder";
|
|
textFieldFloating.delegate = self;
|
|
textFieldFloating.clearButtonMode = UITextFieldViewModeUnlessEditing;
|
|
NSOperatingSystemVersion iOS10Version = {10, 0, 0};
|
|
NSProcessInfo *processInfo = [NSProcessInfo processInfo];
|
|
if ([processInfo respondsToSelector:@selector(isOperatingSystemAtLeastVersion:)] &&
|
|
[processInfo isOperatingSystemAtLeastVersion:iOS10Version]) {
|
|
textFieldFloating.adjustsFontForContentSizeCategory = YES;
|
|
} else {
|
|
[textFieldFloating mdc_setAdjustsFontForContentSizeCategory:YES];
|
|
}
|
|
|
|
self.textFieldControllerFloating =
|
|
[[MDCTextInputController alloc] initWithTextInput:textFieldFloating];
|
|
|
|
self.textFieldControllerFloating.presentationStyle =
|
|
MDCTextInputPresentationStyleFloatingPlaceholder;
|
|
[self.textFieldControllerFloating mdc_setAdjustsFontForContentSizeCategory:YES];
|
|
|
|
[NSLayoutConstraint
|
|
activateConstraints:[NSLayoutConstraint
|
|
constraintsWithVisualFormat:@"V:|-20-[charMax]-[floating]"
|
|
options:NSLayoutFormatAlignAllLeading |
|
|
NSLayoutFormatAlignAllTrailing
|
|
metrics:nil
|
|
views:@{
|
|
@"charMax" : textFieldDefaultCharMax,
|
|
@"floating" : textFieldFloating
|
|
}]];
|
|
[NSLayoutConstraint constraintWithItem:textFieldDefaultCharMax
|
|
attribute:NSLayoutAttributeLeading
|
|
relatedBy:NSLayoutRelationEqual
|
|
toItem:self.view
|
|
attribute:NSLayoutAttributeLeadingMargin
|
|
multiplier:1
|
|
constant:0]
|
|
.active = YES;
|
|
[NSLayoutConstraint constraintWithItem:textFieldDefaultCharMax
|
|
attribute:NSLayoutAttributeTrailing
|
|
relatedBy:NSLayoutRelationEqual
|
|
toItem:self.view
|
|
attribute:NSLayoutAttributeTrailingMargin
|
|
multiplier:1
|
|
constant:0]
|
|
.active = YES;
|
|
|
|
// Full Width Text Field
|
|
MDCTextField *textFieldFullWidth = [[MDCTextField alloc] init];
|
|
[self.scrollView addSubview:textFieldFullWidth];
|
|
textFieldFullWidth.translatesAutoresizingMaskIntoConstraints = NO;
|
|
|
|
textFieldFullWidth.placeholder = @"Full Width";
|
|
textFieldFullWidth.delegate = self;
|
|
textFieldFullWidth.clearButtonMode = UITextFieldViewModeUnlessEditing;
|
|
textFieldFullWidth.backgroundColor = [UIColor whiteColor];
|
|
|
|
self.textFieldControllerFullWidth =
|
|
[[MDCTextInputController alloc] initWithTextInput:textFieldFullWidth];
|
|
|
|
self.textFieldControllerFullWidth.presentationStyle = MDCTextInputPresentationStyleFullWidth;
|
|
[self.textFieldControllerFullWidth mdc_setAdjustsFontForContentSizeCategory:YES];
|
|
|
|
[NSLayoutConstraint constraintWithItem:textFieldFullWidth
|
|
attribute:NSLayoutAttributeTop
|
|
relatedBy:NSLayoutRelationEqual
|
|
toItem:textFieldFloating
|
|
attribute:NSLayoutAttributeBottom
|
|
multiplier:1
|
|
constant:1]
|
|
.active = YES;
|
|
[NSLayoutConstraint constraintWithItem:textFieldFullWidth
|
|
attribute:NSLayoutAttributeLeading
|
|
relatedBy:NSLayoutRelationEqual
|
|
toItem:self.view
|
|
attribute:NSLayoutAttributeLeading
|
|
multiplier:1
|
|
constant:0]
|
|
.active = YES;
|
|
[NSLayoutConstraint constraintWithItem:textFieldFullWidth
|
|
attribute:NSLayoutAttributeTrailing
|
|
relatedBy:NSLayoutRelationEqual
|
|
toItem:self.view
|
|
attribute:NSLayoutAttributeTrailing
|
|
multiplier:1
|
|
constant:0]
|
|
.active = YES;
|
|
|
|
// Unstyled Text Field
|
|
MDCTextField *unstyledTextField = [[MDCTextField alloc] init];
|
|
[self.scrollView addSubview:unstyledTextField];
|
|
unstyledTextField.translatesAutoresizingMaskIntoConstraints = NO;
|
|
|
|
unstyledTextField.placeholder = @"Text Field without Controller";
|
|
unstyledTextField.delegate = self;
|
|
unstyledTextField.clearButtonMode = UITextFieldViewModeAlways;
|
|
|
|
[NSLayoutConstraint constraintWithItem:unstyledTextField
|
|
attribute:NSLayoutAttributeTop
|
|
relatedBy:NSLayoutRelationEqual
|
|
toItem:textFieldFullWidth
|
|
attribute:NSLayoutAttributeBottom
|
|
multiplier:1
|
|
constant:1]
|
|
.active = YES;
|
|
[NSLayoutConstraint constraintWithItem:unstyledTextField
|
|
attribute:NSLayoutAttributeLeading
|
|
relatedBy:NSLayoutRelationEqual
|
|
toItem:textFieldDefaultCharMax
|
|
attribute:NSLayoutAttributeLeading
|
|
multiplier:1
|
|
constant:0]
|
|
.active = YES;
|
|
[NSLayoutConstraint constraintWithItem:unstyledTextField
|
|
attribute:NSLayoutAttributeTrailing
|
|
relatedBy:NSLayoutRelationEqual
|
|
toItem:textFieldDefaultCharMax
|
|
attribute:NSLayoutAttributeTrailing
|
|
multiplier:1
|
|
constant:0]
|
|
.active = YES;
|
|
|
|
// Uncomment for testing
|
|
// UITextField *appleField = [[UITextField alloc] initWithFrame:CGRectZero];
|
|
// [self.scrollView addSubview:appleField];
|
|
// appleField.translatesAutoresizingMaskIntoConstraints = NO;
|
|
//
|
|
// appleField.placeholder = @"UIKit Text Field";
|
|
// appleField.delegate = self;
|
|
// appleField.clearButtonMode = UITextFieldViewModeWhileEditing;
|
|
//
|
|
//
|
|
// NSURL *url = [[NSBundle bundleForClass:[self class]] URLForResource:@"ic_search"
|
|
// withExtension:@"png"];
|
|
// UIImageView *left = [[UIImageView alloc] initWithImage:[UIImage
|
|
// imageWithContentsOfFile:url.path]];
|
|
// UIImageView *right = [[UIImageView alloc] initWithImage:[UIImage
|
|
// imageWithContentsOfFile:url.path]];
|
|
//
|
|
// appleField.leftView = left;
|
|
// appleField.leftViewMode = UITextFieldViewModeAlways;
|
|
// appleField.rightView = right;
|
|
// appleField.rightViewMode = UITextFieldViewModeAlways;
|
|
//
|
|
// [NSLayoutConstraint constraintWithItem:appleField
|
|
// attribute:NSLayoutAttributeTop
|
|
// relatedBy:NSLayoutRelationEqual
|
|
// toItem:unstyledTextField
|
|
// attribute:NSLayoutAttributeBottom
|
|
// multiplier:1
|
|
// constant:10]
|
|
// .active = YES;
|
|
// [NSLayoutConstraint constraintWithItem:appleField
|
|
// attribute:NSLayoutAttributeLeading
|
|
// relatedBy:NSLayoutRelationEqual
|
|
// toItem:textFieldDefaultCharMax
|
|
// attribute:NSLayoutAttributeLeading
|
|
// multiplier:1
|
|
// constant:0]
|
|
// .active = YES;
|
|
// [NSLayoutConstraint constraintWithItem:appleField
|
|
// attribute:NSLayoutAttributeTrailing
|
|
// relatedBy:NSLayoutRelationEqual
|
|
// toItem:textFieldDefaultCharMax
|
|
// attribute:NSLayoutAttributeTrailing
|
|
// multiplier:1
|
|
// constant:0]
|
|
// .active = YES;
|
|
}
|
|
|
|
#pragma mark - UITextFieldDelegate
|
|
|
|
// All the usual UITextFieldDelegate methods work with MDCTextField
|
|
- (BOOL)textField:(UITextField *)textField
|
|
shouldChangeCharactersInRange:(NSRange)range
|
|
replacementString:(NSString *)string {
|
|
return YES;
|
|
}
|
|
|
|
@end
|