Skip to content

Commit

Permalink
Change MDCChipField to use UITextField instead of MDCTextField.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 591304366
  • Loading branch information
CGRect authored and material-automation committed Dec 15, 2023
1 parent 50dba9a commit 7fd4094
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ class ChipsFieldDeleteEnabledViewController: UIViewController, MDCChipFieldDeleg
view.backgroundColor = containerScheming.colorScheme.backgroundColor
chipField.frame = .zero
chipField.delegate = self
chipField.textField.placeholderLabel.text = "This is a chip field."
let placeholderAttributes: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.placeholderText
]
chipField.placeholderAttributes = placeholderAttributes
chipField.placeholder = "This is a chip field."
chipField.backgroundColor = containerScheming.colorScheme.surfaceColor
chipField.showChipsDeleteButton = true
view.addSubview(chipField)
}

Expand Down
35 changes: 13 additions & 22 deletions components/Chips/examples/ChipsInputExampleViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#import "MaterialChips.h"
#import "MaterialChips+Theming.h"
#import "MaterialTextFields.h"
#import "MaterialColorScheme.h"
#import "MaterialContainerScheme.h"
#import "MaterialTypographyScheme.h"
#import "MDCChipField.h"
#import "MDCChipFieldDelegate.h"
#import "MDCChipView.h"
#import "MDCChipView+MaterialTheming.h"
#import "MDCContainerScheme.h"
#import "MDCTypographyScheme.h"

@interface ChipsInputExampleViewController : UIViewController <MDCChipFieldDelegate>
@property(nonatomic, strong) MDCContainerScheme *containerScheme;
Expand All @@ -42,26 +42,17 @@ - (void)viewDidLoad {
typographyScheme.useCurrentContentSizeCategoryWhenApplied = YES;
self.containerScheme.typographyScheme = typographyScheme;

if (self.containerScheme.colorScheme) {
self.view.backgroundColor = self.containerScheme.colorScheme.backgroundColor;
} else {
MDCSemanticColorScheme *colorScheme =
[[MDCSemanticColorScheme alloc] initWithDefaults:MDCColorSchemeDefaultsMaterial201804];
self.view.backgroundColor = colorScheme.backgroundColor;
}
self.view.backgroundColor = UIColor.systemBackgroundColor;

self.chipField = [[MDCChipField alloc] initWithFrame:CGRectZero];
self.chipField.delegate = self;
self.chipField.textField.accessibilityIdentifier = @"chip_field_text_field";
self.chipField.textField.placeholderLabel.text = @"This is a chip field.";
self.chipField.textField.mdc_adjustsFontForContentSizeCategory = YES;
if (self.containerScheme.colorScheme) {
self.chipField.backgroundColor = self.containerScheme.colorScheme.surfaceColor;
} else {
MDCSemanticColorScheme *colorScheme =
[[MDCSemanticColorScheme alloc] initWithDefaults:MDCColorSchemeDefaultsMaterial201804];
self.chipField.backgroundColor = colorScheme.surfaceColor;
}
NSDictionary<NSString *, id> *placeholderAttributes =
@{NSForegroundColorAttributeName : UIColor.placeholderTextColor};
self.chipField.placeholderAttributes = placeholderAttributes;
self.chipField.placeholder = @"This is a chip field.";
self.chipField.textField.adjustsFontForContentSizeCategory = YES;
self.chipField.backgroundColor = UIColor.systemBackgroundColor;
[self.view addSubview:self.chipField];

// When Dynamic Type changes we need to invalidate the collection view layout in order to let the
Expand Down
17 changes: 14 additions & 3 deletions components/Chips/src/MDCChipField.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#import <UIKit/UIKit.h>

#import "MDCChipView.h"
#import "MaterialTextFields.h"

/**
Note: There is a UIKit bug affecting iOS 8.0-8.2 where UITextFields do not properly call the
Expand Down Expand Up @@ -60,7 +59,7 @@ typedef NS_OPTIONS(NSUInteger, MDCChipFieldDelimiter) {

/**
This class provides an "input chips" experience on iOS, where chip creation is
coordinated with a user's text input. It manages an @c MDCTextField and a series of @c
coordinated with a user's text input. It manages a @c UITextField and a series of @c
MDCChipViews. When the user hits the return key, new chips are added. When the client hits the
delete button and the text field has no text, the last chip is deleted.
Expand All @@ -80,7 +79,7 @@ typedef NS_OPTIONS(NSUInteger, MDCChipFieldDelimiter) {
If you set a custom font, make sure to also set the custom font on textField.placeholderLabel and
on your MDCChipView instances.
*/
@property(nonatomic, nonnull, readonly) MDCTextField *textField;
@property(nonatomic, nonnull, readonly) UITextField *textField;

/**
The fixed height of all chip views.
Expand Down Expand Up @@ -142,6 +141,18 @@ typedef NS_OPTIONS(NSUInteger, MDCChipFieldDelimiter) {
*/
@property(nonatomic, nullable, strong) UIImage *deleteButtonImage;

/**
The string to be used as the attributed placeholder for the UITextField associated
with a chip field.
*/
@property(nonatomic, nullable, copy) NSString *placeholder;

/**
The attributes applied to the attributed placeholder in the UITextField associated with a
chip field.
*/
@property(nonatomic, nullable, copy) NSDictionary<NSAttributedStringKey, id> *placeholderAttributes;

/**
Adds a chip to the chip field.
Expand Down
Loading

0 comments on commit 7fd4094

Please sign in to comment.