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

Commit 1f5bec2

Browse files
codeman7material-automation
authored andcommitted
#Buttons Fix dynamic type size for icons
PiperOrigin-RevId: 765030659
1 parent f50ff96 commit 1f5bec2

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

components/M3CButton/src/M3CButton.m

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ @interface M3CButton () {
2929
NSMutableDictionary<NSNumber *, NSValue *> *_edgeInsetsWithImageForSize;
3030
NSMutableDictionary<NSNumber *, NSValue *> *_edgeInsetsWithTitleForSize;
3131
CGSize _visualContentSize;
32+
CGFloat _symbolSize;
3233
BOOL _customInsetAvailable;
3334
BOOL _buttonSizeSet;
3435
}
@@ -274,9 +275,10 @@ - (void)updateSymbolFont {
274275
if (@available(iOS 15.0, *)) {
275276
currentAttributes = _symbolFonts[@(self.buttonSize)];
276277
if (_buttonSizeSet && currentAttributes != nil) {
277-
CGFloat pointSize = [[UIFontMetrics metricsForTextStyle:currentAttributes.textStyle]
278-
scaledValueForValue:currentAttributes.pointSize];
279-
self.imageView.bounds = CGRectMake(0, 0, pointSize, pointSize);
278+
_symbolSize = [[UIFontMetrics metricsForTextStyle:currentAttributes.textStyle]
279+
scaledValueForValue:currentAttributes.pointSize
280+
compatibleWithTraitCollection:self.traitCollection];
281+
self.imageView.bounds = CGRectMake(0, 0, _symbolSize, _symbolSize);
280282
}
281283
}
282284
}
@@ -578,6 +580,13 @@ - (void)didMoveToSuperview {
578580
- (void)traitCollectionDidChange:(nullable UITraitCollection *)previousTraitCollection {
579581
[super traitCollectionDidChange:previousTraitCollection];
580582
[self updateCGColors];
583+
[self updateSymbolFont];
584+
585+
if (@available(iOS 15.0, *)) {
586+
_visualContentSize = [self explicitSize];
587+
}
588+
[self invalidateIntrinsicContentSize];
589+
[self setNeedsLayout];
581590
}
582591

583592
- (void)layoutSubviews {
@@ -763,17 +772,16 @@ - (CGSize)explicitSize API_AVAILABLE(ios(15.0)) {
763772
CGFloat contentEdgeInsetsWidth = self.contentEdgeInsets.left + self.contentEdgeInsets.right;
764773
CGFloat imageEdgeInsetsHeight = self.imageEdgeInsets.top + self.imageEdgeInsets.bottom;
765774
CGFloat contentEdgeInsetsHeight = self.contentEdgeInsets.top + self.contentEdgeInsets.bottom;
766-
CGFloat symbolSize = _symbolFonts[@(self.buttonSize)].pointSize;
767775
if (hasTitle && hasImage) {
768-
CGFloat iconWidth = imageEdgeInsetsWidth + symbolSize;
776+
CGFloat iconWidth = imageEdgeInsetsWidth + _symbolSize;
769777
CGFloat titleWidth = contentEdgeInsetsWidth + self.titleLabel.intrinsicContentSize.width;
770-
CGFloat iconHeight = imageEdgeInsetsHeight + symbolSize;
778+
CGFloat iconHeight = imageEdgeInsetsHeight + _symbolSize;
771779
CGFloat titleHeight = contentEdgeInsetsHeight + self.titleLabel.intrinsicContentSize.height;
772780
size = CGSizeMake(iconWidth + titleWidth, MAX(iconHeight, titleHeight));
773781
} else if (hasImage) {
774782
// If only using an image we set the contentEdgeInsets rather than imageEdgeInsets.
775-
CGFloat width = contentEdgeInsetsWidth + symbolSize;
776-
CGFloat height = contentEdgeInsetsHeight + symbolSize;
783+
CGFloat width = contentEdgeInsetsWidth + _symbolSize;
784+
CGFloat height = contentEdgeInsetsHeight + _symbolSize;
777785
size = CGSizeMake(width, height);
778786
} else if (hasTitle) {
779787
CGFloat width = contentEdgeInsetsWidth + self.titleLabel.intrinsicContentSize.width;

0 commit comments

Comments
 (0)