Skip to content

Commit bfc5984

Browse files
authored
Merge pull request #21 from TimOliver/minimum-width
Added `minimumWidth` property
2 parents 7fbf89e + 55aa673 commit bfc5984

4 files changed

Lines changed: 19 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ x.y.z Release Notes (yyyy-MM-dd)
66

77
### Enchancements
88

9+
* Added `minimumWidth` property to help with guiding external layout. ([#21](https://github.com/TimOliver/TORoundedButton/pull/21))
910
* Refactored button from Core Animation labs chat at WWDC 2019. ([#20](https://github.com/TimOliver/TORoundedButton/pull/20))
1011
* Increased corner radius default value to 12.0. ([#20](https://github.com/TimOliver/TORoundedButton/pull/20))
1112
* Added lower alpha value when button is set to disabled. ([#15](https://github.com/TimOliver/TORoundedButton/pull/15))

TORoundedButton/TORoundedButton.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ IB_DESIGNABLE @interface TORoundedButton : UIControl
3434
@property (nonatomic, copy) IBInspectable NSString *text;
3535

3636
/** The attributed string used in the label of this button. See `UILabel.attributedText` documentation for full details (Default is nil) */
37-
@property (nonatomic, copy, nullable) NSAttributedString *attributedText;
37+
@property (nonatomic, copy, nullable) NSAttributedString *attributedText;
3838

3939
/** The radius of the corners of this button (Default is 12.0f) */
4040
@property (nonatomic, assign) IBInspectable CGFloat cornerRadius;
@@ -63,6 +63,9 @@ IB_DESIGNABLE @interface TORoundedButton : UIControl
6363
/** The duration of the tapping cross-fade animation (Default is 0.4f) */
6464
@property (nonatomic, assign) CGFloat tapAnimationDuration;
6565

66+
/** Given the current size of the text label, the smallest horizontal width in which this button can scale. */
67+
@property (nonatomic, readonly) CGFloat minimumWidth;
68+
6669
/** A callback handler triggered each time the button is tapped. */
6770
@property (nonatomic, copy) void (^tappedHandler)(void);
6871

TORoundedButton/TORoundedButton.m

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ - (void)roundedButtonCommonInit
128128
self.titleLabel.adjustsFontForContentSizeCategory = YES;
129129
self.titleLabel.backgroundColor = self.tintColor;
130130
self.titleLabel.text = @"Button";
131+
[self.titleLabel sizeToFit];
131132
[self.containerView addSubview:self.titleLabel];
132133

133134
// Create action events for all possible interactions with this control
@@ -144,7 +145,6 @@ - (void)layoutSubviews
144145
[super layoutSubviews];
145146

146147
// Configure the button text
147-
[self.titleLabel sizeToFit];
148148
self.titleLabel.center = self.containerView.center;
149149
self.titleLabel.frame = CGRectIntegral(self.titleLabel.frame);
150150
}
@@ -322,6 +322,8 @@ - (void)setButtonScaledTappedAnimated:(BOOL)animated
322322
- (void)setAttributedText:(NSAttributedString *)attributedText
323323
{
324324
self.titleLabel.attributedText = attributedText;
325+
[self.titleLabel sizeToFit];
326+
[self setNeedsLayout];
325327
}
326328

327329
- (NSAttributedString *)attributedText
@@ -332,6 +334,8 @@ - (NSAttributedString *)attributedText
332334
- (void)setText:(NSString *)text
333335
{
334336
self.titleLabel.text = text;
337+
[self.titleLabel sizeToFit];
338+
[self setNeedsLayout];
335339
}
336340
- (NSString *)text { return self.titleLabel.text; }
337341

@@ -404,6 +408,11 @@ - (void)setEnabled:(BOOL)enabled
404408
self.containerView.alpha = enabled ? 1 : 0.4;
405409
}
406410

411+
- (CGFloat)minimumWidth
412+
{
413+
return self.titleLabel.frame.size.width;
414+
}
415+
407416
#pragma mark - Graphics Handling -
408417

409418
+ (UIColor *)brightnessAdjustedColorWithColor:(UIColor *)color amount:(CGFloat)amount

TORoundedButtonExample.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
/* Begin PBXFileReference section */
5656
220F9AAE22784FD4001862A7 /* TORoundedButton.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = TORoundedButton.framework; sourceTree = BUILT_PRODUCTS_DIR; };
5757
220F9ABE2278AA0B001862A7 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
58+
220FC30822BC67E700B5C284 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
59+
220FC30922BC67FE00B5C284 /* CHANGELOG.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = CHANGELOG.md; sourceTree = "<group>"; };
5860
22700639226CA24D003492CB /* TORoundedButtonExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TORoundedButtonExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
5961
2270063C226CA24D003492CB /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
6062
2270063D226CA24D003492CB /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
@@ -110,11 +112,13 @@
110112
22700630226CA24D003492CB = {
111113
isa = PBXGroup;
112114
children = (
115+
220FC30822BC67E700B5C284 /* README.md */,
113116
22700660226CA322003492CB /* TORoundedButton */,
114117
2270063B226CA24D003492CB /* TORoundedButtonExample */,
115118
220F9ABD2278AA0B001862A7 /* TORoundedButtonFramework */,
116119
22700654226CA24E003492CB /* TORoundedButtonExampleTests */,
117120
2270063A226CA24D003492CB /* Products */,
121+
220FC30922BC67FE00B5C284 /* CHANGELOG.md */,
118122
);
119123
sourceTree = "<group>";
120124
};

0 commit comments

Comments
 (0)