material-components_materia.../components/Chips/examples/ChipsInputExampleViewController.m
Cody Weaver d57e6b724f
[Chips] Update example (#5118)
Previously the example didn't respect the safe area or use a typography scheme. This addresses both issues and cleans up some code. You previously couldn't see the chip field because it was hidden behind the app bar.

Closes #3708 

| Before | After |
| ------ | ------ |
|![simulator screen shot - iphone x - 2018-09-13 at 16 28 51](https://user-images.githubusercontent.com/7131294/45513977-259cfc00-b772-11e8-89c7-bcfb65825723.png)|![simulator screen shot - iphone x - 2018-09-13 at 16 27 17](https://user-images.githubusercontent.com/7131294/45513995-30579100-b772-11e8-877d-80c191b781e9.png)|
|![simulator screen shot - iphone x - 2018-09-13 at 16 28 57](https://user-images.githubusercontent.com/7131294/45514052-53824080-b772-11e8-969a-be99d248651e.png)|![simulator screen shot - iphone x - 2018-09-13 at 16 27 48](https://user-images.githubusercontent.com/7131294/45514058-5a10b800-b772-11e8-80fc-5abc602190d5.png)|
2018-09-14 07:25:27 -04:00

83 lines
2.6 KiB
Objective-C

// Copyright 2017-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 "ChipsExamplesSupplemental.h"
#import "MaterialChips.h"
#import "MaterialTextFields.h"
#import "MaterialChips+ChipThemer.h"
@interface ChipsInputExampleViewController () <MDCChipFieldDelegate>
@end
@implementation ChipsInputExampleViewController {
MDCChipField *_chipField;
}
- (id)init {
self = [super init];
if (self) {
_colorScheme = [[MDCSemanticColorScheme alloc] init];
_typographyScheme = [[MDCTypographyScheme alloc] init];
_shapeScheme = [[MDCShapeScheme alloc] init];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = self.colorScheme.backgroundColor;
_chipField = [[MDCChipField alloc] initWithFrame:CGRectZero];
_chipField.delegate = self;
_chipField.textField.placeholderLabel.text = @"This is a chip field.";
_chipField.backgroundColor = self.colorScheme.surfaceColor;
[self.view addSubview:_chipField];
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
CGRect frame = CGRectInset(self.view.bounds, 10, 10);
if (@available(iOS 11.0, *)) {
frame = UIEdgeInsetsInsetRect(frame, self.view.safeAreaInsets);
}
frame.size = [_chipField sizeThatFits:frame.size];
_chipField.frame = frame;
}
- (void)chipFieldHeightDidChange:(MDCChipField *)chipField {
[self.view layoutIfNeeded];
}
- (void)chipField:(MDCChipField *)chipField didAddChip:(MDCChipView *)chip {
MDCChipViewScheme *scheme = [[MDCChipViewScheme alloc] init];
scheme.colorScheme = self.colorScheme;
scheme.typographyScheme = self.typographyScheme;
scheme.shapeScheme = self.shapeScheme;
// Every other chip is stroked
if (chipField.chips.count%2) {
[MDCChipViewThemer applyOutlinedVariantWithScheme:scheme toChipView:chip];
} else {
[MDCChipViewThemer applyScheme:scheme toChipView:chip];
}
[chip sizeToFit];
CGFloat chipVerticalInset = MIN(0, (CGRectGetHeight(chip.bounds) - 48) / 2);
chip.hitAreaInsets = UIEdgeInsetsMake(chipVerticalInset, 0, chipVerticalInset, 0);
}
@end