Skip to content

Commit 5c9ba05

Browse files
Nobodymaterial-automation
authored andcommitted
Internal change now button height defined by the text size.
PiperOrigin-RevId: 713948536
1 parent 0ac0dfe commit 5c9ba05

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

components/Dialogs/src/MDCAlertControllerView.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@
107107
Default value is 12.
108108
*/
109109
@property(nonatomic, assign) CGFloat actionsVerticalMargin;
110-
110+
/**
111+
The vertical space between the action buttons when the buttons are vertically aligned, and if more
112+
than one button is presented.
113+
*/
114+
@property(nonatomic, assign) BOOL adjustButtonVerticalMargin;
111115
/**
112116
The GM3 vertical space between the action buttons when the buttons are
113117
vertically aligned, and if more than one button is presented.

components/Dialogs/src/private/MDCAlertControllerView+Private.m

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#import "MDCTypography.h"
2626
#import "UIFont+MaterialTypography.h"
2727
#import "MDCMath.h"
28-
#import <MDFInternationalization/MDFInternationalization.h>
28+
#import <MDFInternationalization/MDFRTL.h>
2929

3030
NS_ASSUME_NONNULL_BEGIN
3131

@@ -542,7 +542,11 @@ - (CGSize)actionButtonsSizeInVerticalLayout {
542542
UIButton *button = buttons[index];
543543
CGSize buttonSize = [button sizeThatFits:size];
544544
CGFloat minButtonHeight = [self buttonHeight];
545-
buttonSize.height = MAX(buttonSize.height, minButtonHeight);
545+
if (self.adjustButtonVerticalMargin) {
546+
buttonSize.height = MAX(button.titleLabel.frame.size.height, minButtonHeight);
547+
} else {
548+
buttonSize.height = MAX(buttonSize.height, minButtonHeight);
549+
}
546550
size.height += buttonSize.height;
547551
size.width = MAX(size.width, buttonSize.width + widthInset);
548552
if (button != buttons.lastObject) {
@@ -994,11 +998,18 @@ - (void)layoutSubviews {
994998
for (UIButton *button in buttons) {
995999
[button sizeToFit];
9961000
CGRect buttonFrame = button.frame;
1001+
CGFloat buttonTitleHeight = CGRectGetHeight(buttonFrame);
1002+
CGFloat buttonTitleWidth = CGRectGetWidth(buttonFrame);
1003+
if (self.adjustButtonVerticalMargin) {
1004+
buttonTitleHeight = MAX(M3CDialogActionMinHeight, button.titleLabel.frame.size.height);
1005+
button.titleLabel.preferredMaxLayoutWidth = button.titleLabel.frame.size.width;
1006+
buttonTitleWidth = button.intrinsicContentSize.width;
1007+
}
9971008
CGFloat minTouchTargetHeight =
9981009
self.isM3CButtonEnabled ? M3CDialogActionMinHeight : MDCDialogActionMinTouchTarget;
9991010
button.frame = CGRectMake(buttonFrame.origin.x, buttonFrame.origin.y,
1000-
MAX(MDCDialogActionMinTouchTarget, CGRectGetWidth(buttonFrame)),
1001-
MAX(minTouchTargetHeight, CGRectGetHeight(buttonFrame)));
1011+
MAX(MDCDialogActionMinTouchTarget, buttonTitleWidth),
1012+
MAX(minTouchTargetHeight, buttonTitleHeight));
10021013
}
10031014

10041015
if (self.isVerticalActionsLayout) {
@@ -1161,7 +1172,23 @@ - (void)layoutVerticalButtons:(NSArray<UIButton *> *)buttons {
11611172
UIButton *nextButton = buttons[index + 1];
11621173
CGFloat verticalMargin = [self actionsVerticalMarginBetweenTopButton:button
11631174
bottomButton:nextButton];
1164-
buttonCenter.y += multiplier * verticalMargin;
1175+
if (self.adjustButtonVerticalMargin) {
1176+
// Get the font's line height
1177+
CGFloat titleHeight = button.titleLabel.frame.size.height;
1178+
CGFloat lineHeight = button.titleLabel.font.lineHeight;
1179+
1180+
// Check if the title height exceeds the line height (indicating multi-line text)
1181+
BOOL isMultiLine = titleHeight > lineHeight;
1182+
1183+
if (isMultiLine) {
1184+
buttonCenter.y += multiplier * verticalMargin - 3;
1185+
} else {
1186+
buttonCenter.y += multiplier * verticalMargin;
1187+
}
1188+
1189+
} else {
1190+
buttonCenter.y += multiplier * verticalMargin;
1191+
}
11651192
}
11661193
}
11671194

components/Dialogs/tests/unit/MDCAlertControllerLayoutTests.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ - (void)testButtonLayout {
8585

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

0 commit comments

Comments
 (0)