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

Commit 7551027

Browse files
codeman7material-automation
authored andcommitted
#Buttons Update image size to always be square
PiperOrigin-RevId: 757920651
1 parent b29adef commit 7551027

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

components/M3CButton/src/M3CButton.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,13 @@ and edge insets. This method is available in iOS 15.0 and later.
7676
7777
@seealso @c setFont:forSize:
7878
79-
@param symbolFont The font to be applied to the button.
79+
@param symbolSize The initial size of the image.
80+
@param textStyle The scaling curve applied to the image size to support dynamic type.
8081
@param size The size of the button.
8182
*/
82-
- (void)setSymbolFont:(UIFont *)symbolFont forSize:(M3CButtonSize)size API_AVAILABLE(ios(15.0));
83+
- (void)setSymbolSize:(CGFloat)symbolSize
84+
textStyle:(UIFontTextStyle)textStyle
85+
forSize:(M3CButtonSize)size API_AVAILABLE(ios(15.0));
8386

8487
/**
8588
Sets the corner radius for a particular size.

components/M3CButton/src/M3CButton.m

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,29 @@
99

1010
NS_ASSUME_NONNULL_BEGIN
1111

12+
/** Used to store the scaling curve and initial size of the @c imageView of a @c M3CButton. */
13+
@interface M3CIconAttributes : NSObject
14+
15+
@property(nonatomic, copy, readonly) UIFontTextStyle textStyle;
16+
@property(nonatomic, assign, readonly) CGFloat pointSize;
17+
18+
- (instancetype)initWithTextStyle:(UIFontTextStyle)textStyle pointSize:(CGFloat)pointSize;
19+
20+
@end
21+
22+
@implementation M3CIconAttributes
23+
24+
- (instancetype)initWithTextStyle:(UIFontTextStyle)textStyle pointSize:(CGFloat)pointSize {
25+
self = [super init];
26+
if (self) {
27+
_textStyle = [textStyle copy]; // Use copy for NSString properties
28+
_pointSize = pointSize;
29+
}
30+
return self;
31+
}
32+
33+
@end
34+
1235
// Minimum touch size recommended by Apple:
1336
// https://developer.apple.com/design/human-interface-guidelines/accessibility#Mobility
1437
static const CGFloat kMinimumTouchTarget = 44.f;
@@ -19,7 +42,7 @@ @interface M3CButton () {
1942
NSMutableDictionary<NSNumber *, UIColor *> *_borderColors;
2043
NSMutableDictionary<NSNumber *, MDCShadow *> *_shadows;
2144
NSMutableDictionary<NSNumber *, UIFont *> *_fonts;
22-
NSMutableDictionary<NSNumber *, UIFont *> *_symbolFonts;
45+
NSMutableDictionary<NSNumber *, M3CIconAttributes *> *_symbolFonts;
2346
NSMutableDictionary<NSNumber *, NSNumber *> *_cornerRadius;
2447
NSMutableDictionary<NSNumber *, NSNumber *> *_pressedCornerRadius;
2548
NSMutableDictionary<NSNumber *, NSValue *> *_imageEdgeInsetsForSize;
@@ -161,8 +184,11 @@ - (void)setFont:(UIFont *)font forSize:(M3CButtonSize)size API_AVAILABLE(ios(15.
161184
[self updateFont];
162185
}
163186

164-
- (void)setSymbolFont:(UIFont *)font forSize:(M3CButtonSize)size API_AVAILABLE(ios(15.0)) {
165-
_symbolFonts[@(size)] = font;
187+
- (void)setSymbolSize:(CGFloat)symbolSize
188+
textStyle:(UIFontTextStyle)textStyle
189+
forSize:(M3CButtonSize)size API_AVAILABLE(ios(15.0)) {
190+
_symbolFonts[@(size)] = [[M3CIconAttributes alloc] initWithTextStyle:textStyle
191+
pointSize:symbolSize];
166192

167193
[self updateSymbolFont];
168194
}
@@ -264,14 +290,14 @@ - (void)updateFont {
264290
}
265291

266292
- (void)updateSymbolFont {
267-
UIFont *currentFont = nil;
293+
M3CIconAttributes *currentAttributes = nil;
268294

269295
if (@available(iOS 15.0, *)) {
270-
currentFont = _symbolFonts[@(self.buttonSize)];
271-
if (_buttonSizeSet && !(currentFont == nil)) {
272-
[self setPreferredSymbolConfiguration:[UIImageSymbolConfiguration
273-
configurationWithFont:currentFont]
274-
forImageInState:UIControlStateNormal];
296+
currentAttributes = _symbolFonts[@(self.buttonSize)];
297+
if (_buttonSizeSet && currentAttributes != nil) {
298+
CGFloat pointSize = [[UIFontMetrics metricsForTextStyle:currentAttributes.textStyle]
299+
scaledValueForValue:currentAttributes.pointSize];
300+
self.imageView.bounds = CGRectMake(0, 0, pointSize, pointSize);
275301
}
276302
}
277303
}
@@ -578,6 +604,7 @@ - (void)layoutSubviews {
578604
[super layoutSubviews];
579605
[self setCapsuleCornersBasedOn:self.frame.size];
580606
[self updateShadows];
607+
[self updateSymbolFont];
581608

582609
if (_buttonSizeSet) {
583610
if (_visualContentSize.width < kMinimumTouchTarget ||

0 commit comments

Comments
 (0)