From 1f10840db80558591d17e0889478fda3a6431657 Mon Sep 17 00:00:00 2001 From: Tim Oliver Date: Sat, 7 Oct 2023 00:22:17 +0900 Subject: [PATCH 1/7] Added blur view properties to public interface --- TORoundedButton/TORoundedButton.h | 6 ++++++ TORoundedButton/TORoundedButton.m | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/TORoundedButton/TORoundedButton.h b/TORoundedButton/TORoundedButton.h index 70ade14..becdab5 100644 --- a/TORoundedButton/TORoundedButton.h +++ b/TORoundedButton/TORoundedButton.h @@ -39,6 +39,12 @@ IB_DESIGNABLE @interface TORoundedButton : UIControl /// (Default value is 15 points inset from each edge). @property (nonatomic, assign) UIEdgeInsets contentInset; +/// Replaces the solid color background with a blur view. (Default is NO) +@property (nonatomic, assign) BOOL isBlurBackground; + +/// When `isTranslucent` is `YES`, the amount of blur the background view has. +@property (nonatomic, assign) UIBlurEffectStyle blurStyle; + /// The text that is displayed in center of the button (Default is nil). @property (nonatomic, copy, nullable) IBInspectable NSString *text; diff --git a/TORoundedButton/TORoundedButton.m b/TORoundedButton/TORoundedButton.m index 75687f3..0a73d0a 100644 --- a/TORoundedButton/TORoundedButton.m +++ b/TORoundedButton/TORoundedButton.m @@ -103,6 +103,10 @@ - (void)_roundedButtonCommonInit TOROUNDEDBUTTON_OBJC_DIRECT { _tappedButtonScale = (_tappedButtonScale > FLT_EPSILON) ?: 0.97f; _tappedTintColorBrightnessOffset = !TO_ROUNDED_BUTTON_FLOAT_IS_ZERO(_tappedTintColorBrightnessOffset) ?: -0.15f; _contentInset = (UIEdgeInsets){15.0, 15.0, 15.0, 15.0}; + _blurStyle = UIBlurEffectStyleDark; +#ifdef __IPHONE_13_0 + if (@available(iOS 13.0, *)) { _blurStyle = UIBlurEffectStyleSystemMaterialDark; } +#endif // Set the tapped tint color if we've set to dynamically calculate it [self _updateTappedTintColorForTintColor]; @@ -154,6 +158,24 @@ - (void)_makeTitleLabelIfNeeded TOROUNDEDBUTTON_OBJC_DIRECT { [_contentView addSubview:_titleLabel]; } +- (UIView *)_makeBackgroundViewWithBlur:(BOOL)withBlur TOROUNDEDBUTTON_OBJC_DIRECT { + UIView *backgroundView = nil; + if (withBlur) { + UIBlurEffect *const blurEffect = [UIBlurEffect effectWithStyle:_blurStyle]; + backgroundView = [[UIVisualEffectView alloc] initWithEffect:blurEffect]; + } else { + backgroundView = [[UIView alloc] initWithFrame:CGRectZero]; + } + backgroundView = [[UIView alloc] initWithFrame:self.bounds]; + backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; + backgroundView.backgroundColor = self.tintColor; + backgroundView.layer.cornerRadius = _cornerRadius; +#ifdef __IPHONE_13_0 + if (@available(iOS 13.0, *)) { backgroundView.layer.cornerCurve = kCACornerCurveContinuous; } +#endif + return backgroundView; +} + #pragma mark - View Layout - - (void)layoutSubviews { From 36e1ff9f3b1cc6435897745ac3aa91b785e4a8fd Mon Sep 17 00:00:00 2001 From: Tim Oliver Date: Sat, 7 Oct 2023 08:59:38 +0900 Subject: [PATCH 2/7] Add accessor methods to control blur background --- TORoundedButton/TORoundedButton.m | 33 ++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/TORoundedButton/TORoundedButton.m b/TORoundedButton/TORoundedButton.m index 0a73d0a..710fe77 100644 --- a/TORoundedButton/TORoundedButton.m +++ b/TORoundedButton/TORoundedButton.m @@ -120,13 +120,7 @@ - (void)_roundedButtonCommonInit TOROUNDEDBUTTON_OBJC_DIRECT { [self addSubview:_containerView]; // Create the image view which will show the button background - _backgroundView = [[UIView alloc] initWithFrame:self.bounds]; - _backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; - _backgroundView.backgroundColor = self.tintColor; - _backgroundView.layer.cornerRadius = _cornerRadius; -#ifdef __IPHONE_13_0 - if (@available(iOS 13.0, *)) { _backgroundView.layer.cornerCurve = kCACornerCurveContinuous; } -#endif + _backgroundView = [self _makeBackgroundViewWithBlur:_isBlurBackground]; [_containerView addSubview:_backgroundView]; // The foreground content view @@ -489,6 +483,31 @@ - (void)setCornerRadius:(CGFloat)cornerRadius { [self setNeedsLayout]; } +- (void)setIsBlurBackground:(BOOL)isBlurBackground { + if (_isBlurBackground == isBlurBackground) { + return; + } + + _isBlurBackground = isBlurBackground; + [_backgroundView removeFromSuperview]; + _backgroundView = [self _makeBackgroundViewWithBlur:_isBlurBackground]; + [_containerView addSubview:_backgroundView]; +} + +- (void)setBlurStyle:(UIBlurEffectStyle)blurStyle { + if (_blurStyle == blurStyle) { + return; + } + + _blurStyle = blurStyle; + if (!_isBlurBackground || ![_backgroundView isKindOfClass:[UIVisualEffectView class]]) { + return; + } + + UIVisualEffectView *const blurView = (UIVisualEffectView *)_backgroundView; + [blurView setEffect:[UIBlurEffect effectWithStyle:_blurStyle]]; +} + - (void)setEnabled:(BOOL)enabled { [super setEnabled:enabled]; _containerView.alpha = enabled ? 1 : 0.4; From 21373ac7e0778d9fb48d3ee0a6d70100efecd92b Mon Sep 17 00:00:00 2001 From: Tim Oliver Date: Sat, 7 Oct 2023 09:13:12 +0900 Subject: [PATCH 3/7] Renamed API name with a more system standard one --- TORoundedButton/TORoundedButton.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TORoundedButton/TORoundedButton.h b/TORoundedButton/TORoundedButton.h index becdab5..3a6a5e6 100644 --- a/TORoundedButton/TORoundedButton.h +++ b/TORoundedButton/TORoundedButton.h @@ -40,7 +40,7 @@ IB_DESIGNABLE @interface TORoundedButton : UIControl @property (nonatomic, assign) UIEdgeInsets contentInset; /// Replaces the solid color background with a blur view. (Default is NO) -@property (nonatomic, assign) BOOL isBlurBackground; +@property (nonatomic, assign) BOOL isTranslucent; /// When `isTranslucent` is `YES`, the amount of blur the background view has. @property (nonatomic, assign) UIBlurEffectStyle blurStyle; From 32428f5c44f243df08b82dcb8d3558000f2bc33e Mon Sep 17 00:00:00 2001 From: Tim Oliver Date: Sat, 7 Oct 2023 09:14:53 +0900 Subject: [PATCH 4/7] Added accessor logic to control translucent mode --- TORoundedButton/TORoundedButton.m | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/TORoundedButton/TORoundedButton.m b/TORoundedButton/TORoundedButton.m index 710fe77..e774ce7 100644 --- a/TORoundedButton/TORoundedButton.m +++ b/TORoundedButton/TORoundedButton.m @@ -105,7 +105,7 @@ - (void)_roundedButtonCommonInit TOROUNDEDBUTTON_OBJC_DIRECT { _contentInset = (UIEdgeInsets){15.0, 15.0, 15.0, 15.0}; _blurStyle = UIBlurEffectStyleDark; #ifdef __IPHONE_13_0 - if (@available(iOS 13.0, *)) { _blurStyle = UIBlurEffectStyleSystemMaterialDark; } + if (@available(iOS 13.0, *)) { _blurStyle = UIBlurEffectStyleSystemThinMaterialDark; } #endif // Set the tapped tint color if we've set to dynamically calculate it @@ -120,7 +120,7 @@ - (void)_roundedButtonCommonInit TOROUNDEDBUTTON_OBJC_DIRECT { [self addSubview:_containerView]; // Create the image view which will show the button background - _backgroundView = [self _makeBackgroundViewWithBlur:_isBlurBackground]; + _backgroundView = [self _makeBackgroundViewWithBlur:_isTranslucent]; [_containerView addSubview:_backgroundView]; // The foreground content view @@ -157,12 +157,13 @@ - (UIView *)_makeBackgroundViewWithBlur:(BOOL)withBlur TOROUNDEDBUTTON_OBJC_DIRE if (withBlur) { UIBlurEffect *const blurEffect = [UIBlurEffect effectWithStyle:_blurStyle]; backgroundView = [[UIVisualEffectView alloc] initWithEffect:blurEffect]; + backgroundView.clipsToBounds = YES; } else { backgroundView = [[UIView alloc] initWithFrame:CGRectZero]; + backgroundView.backgroundColor = self.tintColor; } - backgroundView = [[UIView alloc] initWithFrame:self.bounds]; + backgroundView.frame = self.bounds; backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; - backgroundView.backgroundColor = self.tintColor; backgroundView.layer.cornerRadius = _cornerRadius; #ifdef __IPHONE_13_0 if (@available(iOS 13.0, *)) { backgroundView.layer.cornerCurve = kCACornerCurveContinuous; } @@ -252,7 +253,7 @@ - (void)_updateTappedTintColorForTintColor TOROUNDEDBUTTON_OBJC_DIRECT { - (UIColor *)_labelBackgroundColor TOROUNDEDBUTTON_OBJC_DIRECT { // Always return clear if tapped - if (_isTapped) { return [UIColor clearColor]; } + if (_isTapped || _isTranslucent) { return [UIColor clearColor]; } // Return clear if the tint color isn't opaque BOOL isClear = CGColorGetAlpha(self.tintColor.CGColor) < (1.0f - FLT_EPSILON); @@ -306,7 +307,7 @@ - (void)_didDragInside { #pragma mark - Animation - - (void)_setBackgroundColorTappedAnimated:(BOOL)animated TOROUNDEDBUTTON_OBJC_DIRECT { - if (!self.tappedTintColor) { return; } + if (!self.tappedTintColor || _isTranslucent) { return; } // Toggle the background color of the title label void (^updateTitleOpacity)(void) = ^{ @@ -483,15 +484,17 @@ - (void)setCornerRadius:(CGFloat)cornerRadius { [self setNeedsLayout]; } -- (void)setIsBlurBackground:(BOOL)isBlurBackground { - if (_isBlurBackground == isBlurBackground) { +- (void)setIsTranslucent:(BOOL)isTranslucent { + if (_isTranslucent == isTranslucent) { return; } - _isBlurBackground = isBlurBackground; + _isTranslucent = isTranslucent; [_backgroundView removeFromSuperview]; - _backgroundView = [self _makeBackgroundViewWithBlur:_isBlurBackground]; - [_containerView addSubview:_backgroundView]; + _backgroundView = [self _makeBackgroundViewWithBlur:_isTranslucent]; + [_containerView insertSubview:_backgroundView atIndex:0]; + _titleLabel.backgroundColor = [self _labelBackgroundColor]; + [self setNeedsLayout]; } - (void)setBlurStyle:(UIBlurEffectStyle)blurStyle { @@ -500,7 +503,7 @@ - (void)setBlurStyle:(UIBlurEffectStyle)blurStyle { } _blurStyle = blurStyle; - if (!_isBlurBackground || ![_backgroundView isKindOfClass:[UIVisualEffectView class]]) { + if (!_isTranslucent || ![_backgroundView isKindOfClass:[UIVisualEffectView class]]) { return; } From 6e6be59c4f2082d0da650e38ecf2f92d410d24a7 Mon Sep 17 00:00:00 2001 From: Tim Oliver Date: Sat, 7 Oct 2023 09:16:29 +0900 Subject: [PATCH 5/7] Add example API for blur background --- TORoundedButtonExample/ViewController.m | 3 +++ 1 file changed, 3 insertions(+) diff --git a/TORoundedButtonExample/ViewController.m b/TORoundedButtonExample/ViewController.m index d41b8b0..8dca209 100644 --- a/TORoundedButtonExample/ViewController.m +++ b/TORoundedButtonExample/ViewController.m @@ -25,6 +25,9 @@ - (void)viewDidLoad { [weakSelf playFadeAnimationOnView:weakSelf.tappedLabel]; }; + // Uncomment to make the background view a blur view + // self.button.isTranslucent = YES; + // Uncomment this line for an attributed string example // self.button.attributedText = [[self class] makeExampleAttributedString]; From a4230443259e3d3f47cf957d5a54114eb0a6bea3 Mon Sep 17 00:00:00 2001 From: Tim Oliver Date: Sat, 7 Oct 2023 09:20:05 +0900 Subject: [PATCH 6/7] Updated CHANGELOG --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f052bbb..de53900 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,9 @@ x.y.z Release Notes (yyyy-MM-dd) ### Added -* A `contentView` property to enable adding custom view content to the button. +* An `isTranslucent` property (and a `blurStyle` property) that replaces the background of buttons from a solid color to a blurred background. +* A `contentView` property to enable adding custom view content to buttons. +* `sizeToFit` and `sizeThatFits:` methods to allow automatic sizing of buttons around their content. ### Fixed From 4b64e5912f04a0cbdf254d499823dcce8e67d700 Mon Sep 17 00:00:00 2001 From: Tim Oliver Date: Sat, 7 Oct 2023 09:20:41 +0900 Subject: [PATCH 7/7] Updated podspec file --- TORoundedButton.podspec | 2 +- TORoundedButtonExample.xcodeproj/project.pbxproj | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/TORoundedButton.podspec b/TORoundedButton.podspec index 4841b8b..1e4302e 100644 --- a/TORoundedButton.podspec +++ b/TORoundedButton.podspec @@ -6,7 +6,7 @@ Pod::Spec.new do |s| s.homepage = 'https://github.com/TimOliver/TORoundedButton' s.author = 'Tim Oliver' s.source = { :git => 'https://github.com/TimOliver/TORoundedButton.git', :tag => s.version } - s.platform = :ios, '10.0' + s.platform = :ios, '12.0' s.source_files = 'TORoundedButton/**/*.{h,m}' s.requires_arc = true end diff --git a/TORoundedButtonExample.xcodeproj/project.pbxproj b/TORoundedButtonExample.xcodeproj/project.pbxproj index 2de098e..4f76b3a 100644 --- a/TORoundedButtonExample.xcodeproj/project.pbxproj +++ b/TORoundedButtonExample.xcodeproj/project.pbxproj @@ -51,6 +51,7 @@ 220FC30822BC67E700B5C284 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; 220FC30922BC67FE00B5C284 /* CHANGELOG.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = CHANGELOG.md; sourceTree = ""; }; 221ACFC52AC939D700DE86FD /* TORoundedButtonExample.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = TORoundedButtonExample.entitlements; sourceTree = ""; }; + 221ACFCF2AD0DAFC00DE86FD /* TORoundedButton.podspec */ = {isa = PBXFileReference; lastKnownFileType = text; path = TORoundedButton.podspec; sourceTree = ""; }; 22700639226CA24D003492CB /* TORoundedButtonExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TORoundedButtonExample.app; sourceTree = BUILT_PRODUCTS_DIR; }; 2270063C226CA24D003492CB /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 2270063D226CA24D003492CB /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; @@ -112,6 +113,7 @@ 2270063A226CA24D003492CB /* Products */, 220FC30922BC67FE00B5C284 /* CHANGELOG.md */, 220FC30822BC67E700B5C284 /* README.md */, + 221ACFCF2AD0DAFC00DE86FD /* TORoundedButton.podspec */, 2274935225849DE500FE4C74 /* Frameworks */, ); sourceTree = "";