Skip to content

Commit 10454d7

Browse files
committed
Added some extra optimizations to the button
1 parent cf63f41 commit 10454d7

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

TORoundedButton/TORoundedButton.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// TORoundedButton.h
33
//
4-
// Copyright 2019-2023 Timothy Oliver. All rights reserved.
4+
// Copyright 2019-2025 Timothy Oliver. All rights reserved.
55
//
66
// Permission is hereby granted, free of charge, to any person obtaining a copy
77
// of this software and associated documentation files (the "Software"), to
@@ -94,8 +94,8 @@ IB_DESIGNABLE @interface TORoundedButton : UIControl
9494
/// (Defaults to off with 1.0f).
9595
@property (nonatomic, assign) IBInspectable CGFloat tappedTextAlpha;
9696

97-
/// Taking the default button background color apply a brightness offset for the tapped color
98-
/// (Default is -0.1f. Set 0.0 for off).
97+
/// Taking the default button background color apply a brightness offset for the tapped color
98+
/// (Default is -0.15f. Set 0.0 for off).
9999
@property (nonatomic, assign) IBInspectable CGFloat tappedTintColorBrightnessOffset;
100100

101101
/// If desired, explicity set the background color of the button when tapped (Default is nil).
@@ -111,14 +111,14 @@ IB_DESIGNABLE @interface TORoundedButton : UIControl
111111
@property (nonatomic, copy) void (^tappedHandler)(void);
112112

113113
/// Create a new instance of a button that can be further configured with either text or custom subviews.
114-
/// The size will be 288 points wide, and 50 tall by default.
114+
/// The size will be 288 points wide, and 52 tall by default.
115115
- (instancetype)init;
116116

117117
/// Create a new instance of a button that can be further configured with either text or custom subviews.
118118
- (instancetype)initWithFrame:(CGRect)frame;
119119

120120
/// Create a new instance of a button with the provided text shown in the center.
121-
/// The size will be 288 points wide, and 50 tall.
121+
/// The size will be 288 points wide, and 52 tall.
122122
- (instancetype)initWithText:(NSString *)text;
123123

124124
/// Create a new instance of a button with the provided view set as the hosting content view.

TORoundedButton/TORoundedButton.m

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// TORoundedButton.m
33
//
4-
// Copyright 2019-2023 Timothy Oliver. All rights reserved.
4+
// Copyright 2019-2025 Timothy Oliver. All rights reserved.
55
//
66
// Permission is hereby granted, free of charge, to any person obtaining a copy
77
// of this software and associated documentation files (the "Software"), to
@@ -32,7 +32,7 @@ static inline BOOL TO_ROUNDED_BUTTON_FLOAT_IS_ZERO(CGFloat value) {
3232
}
3333

3434
static inline BOOL TO_ROUNDED_BUTTON_FLOATS_MATCH(CGFloat firstValue, CGFloat secondValue) {
35-
return fabs(firstValue - secondValue) > FLT_EPSILON;
35+
return fabs(firstValue - secondValue) < FLT_EPSILON;
3636
}
3737

3838
// --------------------------------------------------------------------
@@ -104,10 +104,10 @@ - (instancetype)initWithText:(NSString *)text {
104104

105105
- (void)_roundedButtonCommonInit TOROUNDEDBUTTON_OBJC_DIRECT {
106106
// Default properties (Make sure they're not overriding IB)
107-
_tappedTextAlpha = (_tappedTextAlpha > FLT_EPSILON) ?: 1.0f;
108-
_tapAnimationDuration = (_tapAnimationDuration > FLT_EPSILON) ?: 0.4f;
109-
_tappedButtonScale = (_tappedButtonScale > FLT_EPSILON) ?: 0.97f;
110-
_tappedTintColorBrightnessOffset = !TO_ROUNDED_BUTTON_FLOAT_IS_ZERO(_tappedTintColorBrightnessOffset) ?: -0.15f;
107+
_tappedTextAlpha = (_tappedTextAlpha > FLT_EPSILON) ? _tappedTextAlpha : 1.0f;
108+
_tapAnimationDuration = (_tapAnimationDuration > FLT_EPSILON) ? _tapAnimationDuration : 0.4f;
109+
_tappedButtonScale = (_tappedButtonScale > FLT_EPSILON) ? _tappedButtonScale : 0.97f;
110+
_tappedTintColorBrightnessOffset = !TO_ROUNDED_BUTTON_FLOAT_IS_ZERO(_tappedTintColorBrightnessOffset) ? _tappedTintColorBrightnessOffset : -0.15f;
111111
_contentInset = (UIEdgeInsets){15.0, 15.0, 15.0, 15.0};
112112
_blurStyle = UIBlurEffectStyleDark;
113113

@@ -116,10 +116,10 @@ - (void)_roundedButtonCommonInit TOROUNDEDBUTTON_OBJC_DIRECT {
116116
if (@available(iOS 26.0, *)) {
117117
_cornerConfiguration = [UICornerConfiguration capsuleConfiguration];
118118
} else {
119-
_cornerRadius = (_cornerRadius > FLT_EPSILON) ?: 12.0f;
119+
_cornerRadius = (_cornerRadius > FLT_EPSILON) ? _cornerRadius : 12.0f;
120120
}
121121
#else
122-
_cornerRadius = (_cornerRadius > FLT_EPSILON) ?: 12.0f;
122+
_cornerRadius = (_cornerRadius > FLT_EPSILON) ? _cornerRadius : 12.0f;
123123
#endif
124124

125125
#ifdef __IPHONE_13_0
@@ -149,6 +149,19 @@ - (void)_roundedButtonCommonInit TOROUNDEDBUTTON_OBJC_DIRECT {
149149
[self addTarget:self action:@selector(_didTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
150150
[self addTarget:self action:@selector(_didDragOutside) forControlEvents:UIControlEventTouchDragExit|UIControlEventTouchCancel];
151151
[self addTarget:self action:@selector(_didDragInside) forControlEvents:UIControlEventTouchDragEnter];
152+
153+
// Register for trait changes on iOS 17+
154+
#ifdef __IPHONE_17_0
155+
if (@available(iOS 17.0, *)) {
156+
__weak typeof(self) weakSelf = self;
157+
[self registerForTraitChanges:@[UITraitUserInterfaceStyle.class]
158+
withHandler:^(id<UITraitEnvironment> _Nonnull traitEnvironment,
159+
UITraitCollection * _Nonnull previousCollection) {
160+
[weakSelf setNeedsLayout];
161+
[weakSelf _updateTappedTintColorForTintColor];
162+
}];
163+
}
164+
#endif
152165
}
153166

154167
- (void)_makeTitleLabelIfNeeded TOROUNDEDBUTTON_OBJC_DIRECT {
@@ -189,6 +202,8 @@ - (UIView *)_makeBackgroundViewWithBlur:(BOOL)withBlur TOROUNDEDBUTTON_OBJC_DIRE
189202
} else {
190203
backgroundView.layer.cornerRadius = _cornerRadius;
191204
}
205+
#else
206+
backgroundView.layer.cornerRadius = _cornerRadius;
192207
#endif
193208

194209
#ifdef __IPHONE_13_0
@@ -216,7 +231,14 @@ - (void)layoutSubviews {
216231
UIView *const contentView = _overrideContentView ?: _contentView;
217232
contentView.frame = ({
218233
CGRect frame = contentBounds;
234+
<<<<<<< Updated upstream
219235
frame.size = [contentView sizeThatFits:contentBounds.size];
236+
=======
237+
const CGSize intrinsicContentSize = contentView.intrinsicContentSize;
238+
if (intrinsicContentSize.width > 0.0f && intrinsicContentSize.height > 0.0f) {
239+
frame.size = [contentView sizeThatFits:contentBounds.size];
240+
}
241+
>>>>>>> Stashed changes
220242
frame.origin.x = (boundsSize.width - frame.size.width) * 0.5f;
221243
frame.origin.y = (boundsSize.height - frame.size.height) * 0.5f;
222244
CGRectIntegral(frame);
@@ -244,7 +266,7 @@ - (CGSize)sizeThatFits:(CGSize)size {
244266
const BOOL isMethodOverridden = [_contentView methodForSelector:@selector(sizeThatFits:)] !=
245267
[UIView instanceMethodForSelector:@selector(sizeThatFits:)];
246268
if (isMethodOverridden) {
247-
newSize = [_contentView sizeThatFits:size];
269+
newSize = [_contentView sizeThatFits:contentSize];
248270
} else if (_contentView.subviews.count == 1) {
249271
// When there is 1 view, we can reliably scale the whole view around it.
250272
newSize = [_contentView.subviews.firstObject sizeThatFits:contentSize];
@@ -269,11 +291,19 @@ - (void)tintColorDidChange {
269291
[self setNeedsLayout];
270292
}
271293

294+
// Silence deprecation warning for iOS 17+ (we register for trait changes in init for iOS 17+)
295+
#pragma clang diagnostic push
296+
#pragma clang diagnostic ignored "-Wdeprecated-implementations"
272297
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
273298
[super traitCollectionDidChange:previousTraitCollection];
299+
// On iOS 17+, this is handled by the trait change registration in init
300+
#ifdef __IPHONE_17_0
301+
if (@available(iOS 17.0, *)) { return; }
302+
#endif
274303
[self setNeedsLayout];
275304
[self _updateTappedTintColorForTintColor];
276305
}
306+
#pragma clang diagnostic pop
277307

278308
- (void)_updateTappedTintColorForTintColor TOROUNDEDBUTTON_OBJC_DIRECT {
279309
if (TO_ROUNDED_BUTTON_FLOAT_IS_ZERO(_tappedTintColorBrightnessOffset)) {
@@ -322,7 +352,9 @@ - (void)_didTouchUpInside {
322352

323353
// Broadcast the tap event to all subscribed objects.
324354
if (_tappedHandler) { _tappedHandler(); }
325-
[_delegate roundedButtonDidTap:self];
355+
if ([_delegate respondsToSelector:@selector(roundedButtonDidTap:)]) {
356+
[_delegate roundedButtonDidTap:self];
357+
}
326358
}
327359

328360
- (void)_didDragOutside {

0 commit comments

Comments
 (0)