forked from material-components/material-components-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TextFields] Add some important protocols (material-components#8493)
This PR adds the MDCContainedInputView protocol and the MDCContainedInputViewStyle protocol. MDCContainedInputView is the protocol that all the contained input views conform to. It has properties for all of the things that contained input views should have in common. This includes a style property, which is an object conforming to MDCContainedInputViewStyle. The main job of the style object is to apply various visual decorations on the textfield, like filled backgrounds for filled textfields or outlines for outlined textfields. The style objects also vend the vertical positioning reference objects, which are the things that determine the vertical distances between the textfield's subviews. This makes sense when you consider that the differences in those distances depend on the style of the textfield, i.e. whether it's filled or outlined. Related to #6942.
- Loading branch information
1 parent
55a857f
commit 85bd705
Showing
6 changed files
with
203 additions
and
6 deletions.
There are no files selected for viewing
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
113 changes: 113 additions & 0 deletions
113
components/TextFields/src/ContainedInputView/private/MDCContainedInputView.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,113 @@ | ||
// 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 <Foundation/Foundation.h> | ||
#import <UIKit/UIKit.h> | ||
|
||
#import "MDCContainedInputViewColorViewModel.h" | ||
#import "MDCContainedInputViewLabelAnimation.h" | ||
#import "MDCContainedInputViewLabelState.h" | ||
#import "MDCContainerStyleVerticalPositioningReference.h" | ||
#import "MDCTextControlLabelBehavior.h" | ||
#import "MDCTextControlState.h" | ||
|
||
static const CGFloat kMDCContainedInputViewDefaultAnimationDuration = (CGFloat)0.15; | ||
|
||
@protocol MDCContainedInputViewStyle; | ||
|
||
@protocol MDCContainedInputView <NSObject> | ||
|
||
/** | ||
This object represents the style of the text control, i.e. the thing that makes it filled or | ||
outlined. See the documentation for MDCContainedInputViewStyle for more information on its | ||
responsibilities. | ||
*/ | ||
@property(nonatomic, strong, nonnull) id<MDCContainedInputViewStyle> containerStyle; | ||
|
||
/** | ||
Describes the current @c MDCtextControlState of the view. This value is affected by things like | ||
UIControlState, as well as whether or not it's editing. | ||
*/ | ||
@property(nonatomic, assign, readonly) MDCTextControlState textControlState; | ||
|
||
/** | ||
Describes the current MDCContainedInputViewLabelState of the contained input view. This | ||
value is affected by things like the view's @c textControlState, its @c labelBehavior, and the | ||
text of the floating label. | ||
*/ | ||
@property(nonatomic, assign, readonly) MDCContainedInputViewLabelState labelState; | ||
|
||
/** | ||
Describes the behavior of the label when the view begins editing. | ||
*/ | ||
@property(nonatomic, assign, readonly) MDCTextControlLabelBehavior labelBehavior; | ||
|
||
/** | ||
The @c label is a label that occupies the text area in a resting state with no text and that either | ||
floats above the text or disappears in an editing state. It is distinct from a placeholder. | ||
*/ | ||
@property(strong, nonatomic, readonly, nonnull) UILabel *label; | ||
|
||
/** | ||
The @c normalFont is the contained input view's primary font. The text has this font. The label | ||
also has this font when it isn't floating. | ||
*/ | ||
@property(strong, nonatomic, readonly, nonnull) UIFont *normalFont; | ||
|
||
/** | ||
The @c floatingFont is the font of the label when it's floating. | ||
*/ | ||
@property(strong, nonatomic, readonly, nonnull) UIFont *floatingFont; | ||
|
||
/** | ||
This method returns a MDCContainedInputViewColorViewModel for a given MDCTextControlState. | ||
*/ | ||
- (nonnull MDCContainedInputViewColorViewModel *)containedInputViewColorViewModelForState: | ||
(MDCTextControlState)textControlState; | ||
|
||
/** | ||
This method sets a MDCContainedInputViewColorViewModel for a given MDCTextControlState. | ||
*/ | ||
- (void)setContainedInputViewColorViewModel: | ||
(nonnull MDCContainedInputViewColorViewModel *)containedInputViewColorViewModel | ||
forState:(MDCTextControlState)textFieldState; | ||
|
||
@end | ||
|
||
@protocol MDCContainedInputViewStyle <NSObject> | ||
|
||
/** | ||
This method allows objects conforming to MDCContainedInputViewStyle to apply themselves to objects | ||
conforming to MDCContainedInputView. | ||
*/ | ||
- (void)applyStyleToContainedInputView:(nonnull id<MDCContainedInputView>)containedInputView; | ||
/** | ||
This method allows objects conforming to MDCContainedInputViewStyle to remove the styling | ||
previously applied to objects conforming to MDCContainedInputView. | ||
*/ | ||
- (void)removeStyleFrom:(nonnull id<MDCContainedInputView>)containedInputView; | ||
|
||
/** | ||
The method returns a UIFont for the floating label based on the @c normalFont of the | ||
view. | ||
*/ | ||
- (UIFont *_Nonnull)floatingFontWithNormalFont:(nonnull UIFont *)font; | ||
|
||
/** | ||
This method returns an object that tells the view where to position it's views | ||
vertically. | ||
*/ | ||
- (nonnull id<MDCContainerStyleVerticalPositioningReference>)positioningReference; | ||
|
||
@end |
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
25 changes: 25 additions & 0 deletions
25
components/TextFields/src/ContainedInputView/private/MDCContainedInputViewStyleBase.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,25 @@ | ||
// 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 <Foundation/Foundation.h> | ||
#import <UIKit/UIKit.h> | ||
|
||
#import "MDCContainedInputView.h" | ||
|
||
/** | ||
A base implementation of MDCContainedInputViewStyle. This is only used for base text controls, i.e. | ||
ones that are not filled or outlined. | ||
*/ | ||
@interface MDCContainedInputViewStyleBase : NSObject <MDCContainedInputViewStyle> | ||
@end |
43 changes: 43 additions & 0 deletions
43
components/TextFields/src/ContainedInputView/private/MDCContainedInputViewStyleBase.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,43 @@ | ||
// 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 "MDCContainedInputViewStyleBase.h" | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <UIKit/UIKit.h> | ||
|
||
#import "MDCContainedInputView.h" | ||
#import "MDCContainedInputViewVerticalPositioningGuideBase.h" | ||
|
||
static const CGFloat kBaseFloatingLabelScaleFactor = 0.75; | ||
|
||
@implementation MDCContainedInputViewStyleBase | ||
|
||
- (UIFont *)floatingFontWithNormalFont:(UIFont *)font { | ||
CGFloat scaleFactor = kBaseFloatingLabelScaleFactor; | ||
CGFloat floatingFontSize = font.pointSize * scaleFactor; | ||
return [font fontWithSize:floatingFontSize]; | ||
} | ||
|
||
- (void)applyStyleToContainedInputView:(id<MDCContainedInputView>)inputView { | ||
} | ||
|
||
- (void)removeStyleFrom:(id<MDCContainedInputView>)containedInputView { | ||
} | ||
|
||
- (id<MDCContainerStyleVerticalPositioningReference>)positioningReference { | ||
return [[MDCContainedInputViewVerticalPositioningGuideBase alloc] init]; | ||
} | ||
|
||
@end |
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