Skip to content

Commit

Permalink
Updates chips to use a dynamically calculated height instead of a fix…
Browse files Browse the repository at this point in the history
…ed height.

PiperOrigin-RevId: 676298831
  • Loading branch information
Nobody authored and material-automation committed Sep 19, 2024
1 parent 16e9402 commit c87bdfa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
5 changes: 4 additions & 1 deletion components/Chips/src/MDCChipField.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ typedef NS_OPTIONS(NSUInteger, MDCChipFieldDelimiter) {
Default is YES.
*/
@property(nonatomic, assign) BOOL showPlaceholderWithChips;

/**
If true, the chip will increase its height to accommodate accessibility sizes of text.
*/
@property(nonatomic, assign) BOOL adjustChipHeightForTextSize;
/**
Enabling this property allows chips to be deleted by tapping on them.
Expand Down
19 changes: 14 additions & 5 deletions components/Chips/src/MDCChipField.m
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,15 @@ - (void)layoutSubviews {
}
}

- (CGFloat)getChipHeight {
if (self.adjustChipHeightForTextSize) {
CGFloat chipDynamicHeight = self.textField.font.lineHeight;
return _chipHeight > chipDynamicHeight ? _chipHeight : chipDynamicHeight + 5;
} else {
return _chipHeight;
}
}

- (void)updateTextFieldPlaceholderText {
// There should be no placeholder if showPlaceholderWithChips is NO and there are chips.
if (!self.showPlaceholderWithChips && self.chips.count > 0) {
Expand Down Expand Up @@ -309,7 +318,7 @@ - (CGSize)sizeThatFits:(CGSize)size {
// To properly apply bottom inset: Calculate what would be the height if there were a chip
// instead of the text field. Then add the bottom inset.
CGFloat height = CGRectGetMaxY(textFieldFrame) + self.contentEdgeInsets.bottom +
(self.chipHeight - textFieldFrame.size.height) / 2;
([self getChipHeight] - textFieldFrame.size.height) / 2;
CGFloat width = MAX(size.width, self.minTextFieldWidth);

return CGSizeMake(width, height);
Expand Down Expand Up @@ -688,7 +697,7 @@ - (BOOL)isTextFieldEmpty {
CGFloat currentOriginX = self.contentEdgeInsets.left;

for (MDCChipView *chip in self.chips) {
CGSize chipSize = [chip sizeThatFits:CGSizeMake(maxWidth, self.chipHeight)];
CGSize chipSize = [chip sizeThatFits:CGSizeMake(maxWidth, [self getChipHeight])];
chipSize.width = MIN(chipSize.width, maxWidth);

CGFloat availableWidth = chipFieldMaxX - currentOriginX;
Expand All @@ -700,7 +709,7 @@ - (BOOL)isTextFieldEmpty {
currentOriginX = self.contentEdgeInsets.left;
}
CGFloat currentOriginY =
self.contentEdgeInsets.top + (row * (self.chipHeight + MDCChipFieldVerticalMargin));
self.contentEdgeInsets.top + (row * ([self getChipHeight] + MDCChipFieldVerticalMargin));
CGRect chipFrame = CGRectMake(currentOriginX, currentOriginY, chipSize.width, chipSize.height);
[chipFrames addObject:[NSValue valueWithCGRect:chipFrame]];
currentOriginX = CGRectGetMaxX(chipFrame) + MDCChipFieldHorizontalMargin;
Expand All @@ -712,7 +721,7 @@ - (CGRect)frameForTextFieldForLastChipFrame:(CGRect)lastChipFrame
chipFieldSize:(CGSize)chipFieldSize {
CGFloat availableWidth = [self availableWidthForTextInput];
CGFloat textFieldHeight = [self.textField sizeThatFits:chipFieldSize].height;
CGFloat originY = lastChipFrame.origin.y + (self.chipHeight - textFieldHeight) / 2;
CGFloat originY = lastChipFrame.origin.y + ([self getChipHeight] - textFieldHeight) / 2;

// If no chip exists, make the text field the full width, adjusted for insets.
if (CGRectIsEmpty(lastChipFrame)) {
Expand All @@ -725,7 +734,7 @@ - (CGRect)frameForTextFieldForLastChipFrame:(CGRect)lastChipFrame
CGFloat desiredTextWidth = [self textInputDesiredWidth];
if (availableWidth < desiredTextWidth) {
// The text field doesn't fit on the line with the last chip.
originY += self.chipHeight + MDCChipFieldVerticalMargin;
originY += [self getChipHeight] + MDCChipFieldVerticalMargin;
originX = self.contentEdgeInsets.left;
textFieldWidth =
chipFieldSize.width - self.contentEdgeInsets.left - self.contentEdgeInsets.right;
Expand Down

0 comments on commit c87bdfa

Please sign in to comment.