Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Chips] Move MDCChipField text input to a new line if text is too wide #9845

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[Chips] Update MDCChipField to reposition textField while typing
  • Loading branch information
bryanoltman committed Mar 5, 2020
commit 5acc4292406ccfb49039b515b1cc3bf5f84b6671
16 changes: 16 additions & 0 deletions components/Chips/src/MDCChipField.m
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,22 @@ - (void)textFieldDidChange {
[self deselectAllChips];
[self createNewChipWithTextField:self.textField delimiter:MDCChipFieldDelimiterSpace];

CGRect lastChipFrame = self.chips.lastObject.frame;
if (!CGRectIsEmpty(lastChipFrame)) {
BOOL isTextTooWide = [self textInputDesiredWidth] >= [self availableWidthForTextInput];
BOOL isTextFieldOnSameLineAsChips =
CGRectGetMidY(self.textField.frame) == CGRectGetMidY(lastChipFrame);
if (isTextTooWide && isTextFieldOnSameLineAsChips) {
// The text is on the same line as the chips and doesn't fit
// Trigger layout to move the text field down to the next line
[self setNeedsLayout];
} else if (!isTextTooWide && !isTextFieldOnSameLineAsChips) {
// The text is on the line below the chips but can fit on the same line
// Trigger layout to move the text field up to the previous line
[self setNeedsLayout];
}
}

if ([self.delegate respondsToSelector:@selector(chipField:didChangeInput:)]) {
[self.delegate chipField:self didChangeInput:[self.textField.text copy]];
}
Expand Down