Skip to content

Commit

Permalink
[Buttons] Add extra width to buttons with text and image when UIAcces…
Browse files Browse the repository at this point in the history
…sibilityIsBoldTextEnabled is true.

This is a workaround for a UIKit bug where buttons with text and an image were truncating their text, despite having plenty of available width.

PiperOrigin-RevId: 339336058
  • Loading branch information
bryanoltman authored and material-automation committed Oct 27, 2020
1 parent 0c6adf0 commit cf28c81
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions components/Buttons/src/MDCButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,20 @@ - (CGSize)sizeThatFits:(CGSize)size {
CGSize givenSizeWithInsets = CGSizeShrinkWithInsets(size, _visibleAreaInsets);
CGSize superSize = [super sizeThatFits:givenSizeWithInsets];

// TODO(b/171816831): revisit this in a future iOS version to verify reproducibility.
// Because of a UIKit bug in iOS 13 and 14 (current), buttons that have both an image and text
// will not return an appropriately large size from [super sizeThatFits:]. In this case, we need
// to expand the width. The number 1 was chosen somewhat arbitrarily, but based on some spot
// testing, adding the smallest amount of extra width possible seems to fix the issue.
#if defined(__IPHONE_13_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0)
if (@available(iOS 13.0, *)) {
if (UIAccessibilityIsBoldTextEnabled() && [self imageForState:UIControlStateNormal] &&
[self titleForState:UIControlStateNormal]) {
superSize.width += 1;
}
}
#endif

if (self.minimumSize.height > 0) {
superSize.height = MAX(self.minimumSize.height, superSize.height);
}
Expand Down

0 comments on commit cf28c81

Please sign in to comment.