Skip to content
This repository was archived by the owner on Dec 11, 2025. It is now read-only.

Commit e28f9c5

Browse files
codeman7material-automation
authored andcommitted
#Buttons Add explicitSize method
PiperOrigin-RevId: 758372107
1 parent 7551027 commit e28f9c5

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

components/M3CButton/src/M3CButton.m

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,9 @@ - (CGSize)intrinsicContentSize {
732732
}
733733
CGSize clampToMinimumSize = [self clampToMinimumSize:size];
734734
if (_buttonSizeSet) {
735+
if (@available(iOS 15.0, *)) {
736+
size = [self explicitSize];
737+
}
735738
_visualContentSize = size;
736739
return CGSizeMake(MAX(kMinimumTouchTarget, clampToMinimumSize.width),
737740
MAX(kMinimumTouchTarget, clampToMinimumSize.height));
@@ -750,6 +753,9 @@ - (CGSize)sizeThatFits:(CGSize)size {
750753
CGSize clampToMinimumSize = [self clampToMinimumSize:newSize];
751754
[self setCapsuleCornersBasedOn:clampToMinimumSize];
752755
if (_buttonSizeSet) {
756+
if (@available(iOS 15.0, *)) {
757+
newSize = [self explicitSize];
758+
}
753759
_visualContentSize = newSize;
754760
return CGSizeMake(MAX(kMinimumTouchTarget, clampToMinimumSize.width),
755761
MAX(kMinimumTouchTarget, clampToMinimumSize.height));
@@ -758,6 +764,36 @@ - (CGSize)sizeThatFits:(CGSize)size {
758764
}
759765
}
760766

767+
- (CGSize)explicitSize API_AVAILABLE(ios(15.0)) {
768+
[self updateInsets];
769+
BOOL hasTitle = self.currentTitle.length > 0 || self.currentAttributedTitle.length > 0;
770+
BOOL hasImage = self.currentImage != nil;
771+
CGSize size = [super intrinsicContentSize];
772+
CGFloat imageEdgeInsetsWidth = self.imageEdgeInsets.left + self.imageEdgeInsets.right;
773+
CGFloat contentEdgeInsetsWidth = self.contentEdgeInsets.left + self.contentEdgeInsets.right;
774+
CGFloat imageEdgeInsetsHeight = self.imageEdgeInsets.top + self.imageEdgeInsets.bottom;
775+
CGFloat contentEdgeInsetsHeight = self.contentEdgeInsets.top + self.contentEdgeInsets.bottom;
776+
CGFloat symbolSize = _symbolFonts[@(self.buttonSize)].pointSize;
777+
if (hasTitle && hasImage) {
778+
CGFloat iconWidth = imageEdgeInsetsWidth + symbolSize;
779+
CGFloat titleWidth = contentEdgeInsetsWidth + self.titleLabel.intrinsicContentSize.width;
780+
CGFloat iconHeight = imageEdgeInsetsHeight + symbolSize;
781+
CGFloat titleHeight = contentEdgeInsetsHeight + self.titleLabel.intrinsicContentSize.height;
782+
size = CGSizeMake(iconWidth + titleWidth, MAX(iconHeight, titleHeight));
783+
} else if (hasImage) {
784+
// If only using an image we set the contentEdgeInsets rather than imageEdgeInsets.
785+
CGFloat width = contentEdgeInsetsWidth + symbolSize;
786+
CGFloat height = contentEdgeInsetsHeight + symbolSize;
787+
size = CGSizeMake(width, height);
788+
} else if (hasTitle) {
789+
CGFloat width = contentEdgeInsetsWidth + self.titleLabel.intrinsicContentSize.width;
790+
CGFloat height = contentEdgeInsetsHeight + self.titleLabel.intrinsicContentSize.height;
791+
size = CGSizeMake(width, height);
792+
}
793+
794+
return size;
795+
}
796+
761797
#pragma mark - CALayerDelegate
762798

763799
- (nullable id<CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)key {

0 commit comments

Comments
 (0)