Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion TORoundedButton.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions TORoundedButton/TORoundedButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 isTranslucent;

/// 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;

Expand Down
62 changes: 53 additions & 9 deletions TORoundedButton/TORoundedButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 = UIBlurEffectStyleSystemThinMaterialDark; }
#endif

// Set the tapped tint color if we've set to dynamically calculate it
[self _updateTappedTintColorForTintColor];
Expand All @@ -116,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:_isTranslucent];
[_containerView addSubview:_backgroundView];

// The foreground content view
Expand Down Expand Up @@ -154,6 +152,25 @@ - (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];
backgroundView.clipsToBounds = YES;
} else {
backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
backgroundView.backgroundColor = self.tintColor;
}
backgroundView.frame = self.bounds;
backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
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 {
Expand Down Expand Up @@ -236,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);
Expand Down Expand Up @@ -290,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) = ^{
Expand Down Expand Up @@ -467,6 +484,33 @@ - (void)setCornerRadius:(CGFloat)cornerRadius {
[self setNeedsLayout];
}

- (void)setIsTranslucent:(BOOL)isTranslucent {
if (_isTranslucent == isTranslucent) {
return;
}

_isTranslucent = isTranslucent;
[_backgroundView removeFromSuperview];
_backgroundView = [self _makeBackgroundViewWithBlur:_isTranslucent];
[_containerView insertSubview:_backgroundView atIndex:0];
_titleLabel.backgroundColor = [self _labelBackgroundColor];
[self setNeedsLayout];
}

- (void)setBlurStyle:(UIBlurEffectStyle)blurStyle {
if (_blurStyle == blurStyle) {
return;
}

_blurStyle = blurStyle;
if (!_isTranslucent || ![_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;
Expand Down
2 changes: 2 additions & 0 deletions TORoundedButtonExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
220FC30822BC67E700B5C284 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
220FC30922BC67FE00B5C284 /* CHANGELOG.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = CHANGELOG.md; sourceTree = "<group>"; };
221ACFC52AC939D700DE86FD /* TORoundedButtonExample.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = TORoundedButtonExample.entitlements; sourceTree = "<group>"; };
221ACFCF2AD0DAFC00DE86FD /* TORoundedButton.podspec */ = {isa = PBXFileReference; lastKnownFileType = text; path = TORoundedButton.podspec; sourceTree = "<group>"; };
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 = "<group>"; };
2270063D226CA24D003492CB /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -112,6 +113,7 @@
2270063A226CA24D003492CB /* Products */,
220FC30922BC67FE00B5C284 /* CHANGELOG.md */,
220FC30822BC67E700B5C284 /* README.md */,
221ACFCF2AD0DAFC00DE86FD /* TORoundedButton.podspec */,
2274935225849DE500FE4C74 /* Frameworks */,
);
sourceTree = "<group>";
Expand Down
3 changes: 3 additions & 0 deletions TORoundedButtonExample/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down