99
1010NS_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
1437static 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