From 19083f035d394ef50dd837974ae3a5b1474c7d40 Mon Sep 17 00:00:00 2001 From: Tim Oliver Date: Mon, 2 Oct 2023 00:58:47 +0900 Subject: [PATCH] Added additional properties to header --- TORoundedButton/TORoundedButton.h | 11 +++++++++++ TORoundedButton/TORoundedButton.m | 3 +++ TORoundedButtonExample/Base.lproj/Main.storyboard | 8 ++++---- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/TORoundedButton/TORoundedButton.h b/TORoundedButton/TORoundedButton.h index a5997ad..44f6c50 100644 --- a/TORoundedButton/TORoundedButton.h +++ b/TORoundedButton/TORoundedButton.h @@ -35,6 +35,10 @@ IB_DESIGNABLE @interface TORoundedButton : UIControl /// this property to your own custom UIView subclass in order to more efficiently manage sizing and layout. @property (nonatomic, strong, null_resettable) UIView *contentView; +/// The amount of inset padding between the content view and the edges of the button. +/// (Default value is 15 points inset from each edge). +@property (nonatomic, assign) UIEdgeInsets contentInset; + /// The text that is displayed in center of the button (Default is nil). @property (nonatomic, copy, nullable) IBInspectable NSString *text; @@ -90,6 +94,13 @@ IB_DESIGNABLE @interface TORoundedButton : UIControl /// Create a new instance of a button with the provided view set as the hosting content view. - (instancetype)initWithContentView:(__kindof UIView *)contentView; +/// Resizes the button to fit the bounding size of all of the subviews in `contentView`, plus content insetting. +/// If a custom view was provided as the content view, or if the content view only has one subview this will also be called on it. +- (void)sizeToFit; + +/// Calculates and returns the appropriate minimum size this button needs to be to fit into the provided size. +- (CGSize)sizeThatFits:(CGSize)size; + @end NS_ASSUME_NONNULL_END diff --git a/TORoundedButton/TORoundedButton.m b/TORoundedButton/TORoundedButton.m index b3e27d2..6e45486 100644 --- a/TORoundedButton/TORoundedButton.m +++ b/TORoundedButton/TORoundedButton.m @@ -99,6 +99,7 @@ - (void)_roundedButtonCommonInit TOROUNDEDBUTTON_OBJC_DIRECT { _tapAnimationDuration = (_tapAnimationDuration > FLT_EPSILON) ?: 0.4f; _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}; // Set the tapped tint color if we've set to dynamically calculate it [self _updateTappedTintColorForTintColor]; @@ -348,6 +349,7 @@ - (void)setContentView:(UIView *)contentView { } - (void)setAttributedText:(NSAttributedString *)attributedText { + [self _makeTitleLabelIfNeeded]; _titleLabel.attributedText = attributedText; [_titleLabel sizeToFit]; [self setNeedsLayout]; @@ -356,6 +358,7 @@ - (void)setAttributedText:(NSAttributedString *)attributedText { - (NSAttributedString *)attributedText { return _titleLabel.attributedText; } - (void)setText:(NSString *)text { + [self _makeTitleLabelIfNeeded]; _titleLabel.text = text; [_titleLabel sizeToFit]; [self setNeedsLayout]; diff --git a/TORoundedButtonExample/Base.lproj/Main.storyboard b/TORoundedButtonExample/Base.lproj/Main.storyboard index e0a4987..9bb8f0a 100644 --- a/TORoundedButtonExample/Base.lproj/Main.storyboard +++ b/TORoundedButtonExample/Base.lproj/Main.storyboard @@ -1,9 +1,9 @@ - + - + @@ -18,7 +18,7 @@ - + @@ -29,7 +29,7 @@