Skip to content

Commit

Permalink
Internal change now button height defined by the text size.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 713948536
  • Loading branch information
Nobody authored and material-automation committed Jan 10, 2025
1 parent 0ac0dfe commit 5c9ba05
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
6 changes: 5 additions & 1 deletion components/Dialogs/src/MDCAlertControllerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@
Default value is 12.
*/
@property(nonatomic, assign) CGFloat actionsVerticalMargin;

/**
The vertical space between the action buttons when the buttons are vertically aligned, and if more
than one button is presented.
*/
@property(nonatomic, assign) BOOL adjustButtonVerticalMargin;
/**
The GM3 vertical space between the action buttons when the buttons are
vertically aligned, and if more than one button is presented.
Expand Down
37 changes: 32 additions & 5 deletions components/Dialogs/src/private/MDCAlertControllerView+Private.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#import "MDCTypography.h"
#import "UIFont+MaterialTypography.h"
#import "MDCMath.h"
#import <MDFInternationalization/MDFInternationalization.h>
#import <MDFInternationalization/MDFRTL.h>

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -542,7 +542,11 @@ - (CGSize)actionButtonsSizeInVerticalLayout {
UIButton *button = buttons[index];
CGSize buttonSize = [button sizeThatFits:size];
CGFloat minButtonHeight = [self buttonHeight];
buttonSize.height = MAX(buttonSize.height, minButtonHeight);
if (self.adjustButtonVerticalMargin) {
buttonSize.height = MAX(button.titleLabel.frame.size.height, minButtonHeight);
} else {
buttonSize.height = MAX(buttonSize.height, minButtonHeight);
}
size.height += buttonSize.height;
size.width = MAX(size.width, buttonSize.width + widthInset);
if (button != buttons.lastObject) {
Expand Down Expand Up @@ -994,11 +998,18 @@ - (void)layoutSubviews {
for (UIButton *button in buttons) {
[button sizeToFit];
CGRect buttonFrame = button.frame;
CGFloat buttonTitleHeight = CGRectGetHeight(buttonFrame);
CGFloat buttonTitleWidth = CGRectGetWidth(buttonFrame);
if (self.adjustButtonVerticalMargin) {
buttonTitleHeight = MAX(M3CDialogActionMinHeight, button.titleLabel.frame.size.height);
button.titleLabel.preferredMaxLayoutWidth = button.titleLabel.frame.size.width;
buttonTitleWidth = button.intrinsicContentSize.width;
}
CGFloat minTouchTargetHeight =
self.isM3CButtonEnabled ? M3CDialogActionMinHeight : MDCDialogActionMinTouchTarget;
button.frame = CGRectMake(buttonFrame.origin.x, buttonFrame.origin.y,
MAX(MDCDialogActionMinTouchTarget, CGRectGetWidth(buttonFrame)),
MAX(minTouchTargetHeight, CGRectGetHeight(buttonFrame)));
MAX(MDCDialogActionMinTouchTarget, buttonTitleWidth),
MAX(minTouchTargetHeight, buttonTitleHeight));
}

if (self.isVerticalActionsLayout) {
Expand Down Expand Up @@ -1161,7 +1172,23 @@ - (void)layoutVerticalButtons:(NSArray<UIButton *> *)buttons {
UIButton *nextButton = buttons[index + 1];
CGFloat verticalMargin = [self actionsVerticalMarginBetweenTopButton:button
bottomButton:nextButton];
buttonCenter.y += multiplier * verticalMargin;
if (self.adjustButtonVerticalMargin) {
// Get the font's line height
CGFloat titleHeight = button.titleLabel.frame.size.height;
CGFloat lineHeight = button.titleLabel.font.lineHeight;

// Check if the title height exceeds the line height (indicating multi-line text)
BOOL isMultiLine = titleHeight > lineHeight;

if (isMultiLine) {
buttonCenter.y += multiplier * verticalMargin - 3;
} else {
buttonCenter.y += multiplier * verticalMargin;
}

} else {
buttonCenter.y += multiplier * verticalMargin;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ - (void)testButtonLayout {

// The vertical layout should be approximately twice as high as the horizontal layout, +/-
// padding between buttons.
XCTAssertEqualWithAccuracy(sizeOnFourInchLandscape.height * 2, sizeOnFourInchPortrait.height, 20);
XCTAssertEqualWithAccuracy(sizeOnFourInchLandscape.height * 2, sizeOnFourInchPortrait.height, 40);
XCTAssertGreaterThan(sizeOnFourInchLandscape.width, sizeOnFourInchPortrait.width);
}

Expand Down

0 comments on commit 5c9ba05

Please sign in to comment.