Skip to content
This repository has been archived by the owner on May 4, 2023. It is now read-only.

Commit

Permalink
Adding CocoaPods support, updating README.
Browse files Browse the repository at this point in the history
  • Loading branch information
bdbergeron committed Jan 16, 2014
1 parent 865e7f5 commit 3341af1
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 26 deletions.
15 changes: 15 additions & 0 deletions BTButton.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Pod::Spec.new do |s|
s.name = 'BTButton'
s.version = '1.0.0'
s.license = 'MIT'
s.summary = 'A UIButton subclass with easy button styling!'
s.homepage = 'https://github.com/bdbergeron/BTButton'
s.authors = { 'Bradley David Bergeron' => '[email protected]' }
s.source = { :git => 'https://github.com/bdbergeron/BTButton.git', :tag => '0.1.0' }
s.requires_arc = true

s.platform = :ios, '7.0'

s.source_files = 'BTButton/*.{h,m}'
s.public_header_files = 'BTButton/*.h'
end
9 changes: 1 addition & 8 deletions BTButton/BTButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,13 @@

@import UIKit;


FOUNDATION_EXPORT NSString * const BTButtonCornerRadiusName;
FOUNDATION_EXPORT NSString * const BTButtonFillColorName;
FOUNDATION_EXPORT NSString * const BTButtonBorderColorName;
FOUNDATION_EXPORT NSString * const BTButtonBorderWidthName;


typedef NS_ENUM(NSUInteger, BTButtonStyle)
{
BTButtonStyleNormal,
BTButtonStyleOutline,
BTButtonStyleFill
};


#pragma mark -
@interface BTButton : UIButton

Expand Down
36 changes: 19 additions & 17 deletions BTButton/BTButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ - (NSDictionary *)styleAttributesForControlState:(UIControlState)state
}

#pragma mark Background Images
static inline NSString *btbutton_NSStringFromUIColor(UIColor *color)
{
if (!color)
return nil;

const CGFloat *c = CGColorGetComponents(color.CGColor);
if (CGColorGetNumberOfComponents(color.CGColor) == 2)
return [NSString stringWithFormat:@"{%f, %f}", c[0], c[1]];
else
return [NSString stringWithFormat:@"{%f, %f, %f, %f}", c[0], c[1], c[2], c[3]];
}

- (UIImage *)btbutton_backgroundImageForState:(UIControlState)state
{
static NSCache *_backgroundImages;
Expand All @@ -98,17 +110,19 @@ - (UIImage *)btbutton_backgroundImageForState:(UIControlState)state

CGFloat borderWidth = [attributes[BTButtonBorderWidthName] floatValue];

NSString *identifier = [NSString stringWithFormat:@"%@BackgroundImage_r%0.2f_f%@_b%@_w%0.2f",
NSStringFromClass([self class]), radius,
btbutton_NSStringFromUIColor(fillColor), btbutton_NSStringFromUIColor(borderColor),
borderWidth];
NSString *identifier =
[NSString stringWithFormat:@"BTButtonBackgroundImage_r%0.2f_f%@_b%@_w%0.2f", radius,
btbutton_NSStringFromUIColor(fillColor), btbutton_NSStringFromUIColor(borderColor),
borderWidth];

UIImage *image = [_backgroundImages objectForKey:identifier];
if (!image)
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:radius];
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds
cornerRadius:radius];
CGContextAddPath(context, path.CGPath);
CGContextClip(context);

Expand All @@ -127,16 +141,4 @@ - (UIImage *)btbutton_backgroundImageForState:(UIControlState)state
return image;
}

static inline NSString *btbutton_NSStringFromUIColor(UIColor *color)
{
if (!color)
return nil;

const CGFloat *c = CGColorGetComponents(color.CGColor);
if (CGColorGetNumberOfComponents(color.CGColor) == 2)
return [NSString stringWithFormat:@"{%f, %f}", c[0], c[1]];
else
return [NSString stringWithFormat:@"{%f, %f, %f, %f}", c[0], c[1], c[2], c[3]];
}

@end
53 changes: 52 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,54 @@
# BTButton

BTButton is a UIButton subclass for iOS 7. With BTButton, you can create round buttons and rounded rect buttons with ease.
BTButton is a UIButton subclass for iOS 7. Although many argue that one should not subclass UIButton, all BTButton does is add a couple methods that make styling buttons in iOS applcations much simpler.

Take a look at the included demo app in order to get a full understanding of how BTButton works.

## Installation

To use BTButton in you projects, you can simply add `pod BTButton, '~> 1.0.0'` to your Podfile, if you're using CocoaPods. If you prefer the manual route, just add the contents of the BTButton folder to your project. Super simple!

## Usage

Taking inspiration from Apple's push to using NSAttributedStrings throughout iOS 7, BTButton uses one simple method to set the style of a button. The currently supported customizations are:

* Corner Radius
* Fill Color
* Border Color
* Border Width

By using different combinations of these attributes, you can create a handful of different effects, such as a red button with a white label and a 5 pixel corner radius:

```obj-c
NSAttributedString *title =
[[NSAttributedString alloc] initWithString:@"Button"
attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];

BTButton *button = [BTButton new];
[button setStyleAttributes:@{BTButtonCornerRadiusName:@(5.0f),
BTButtonFillColorName:[UIColor red]}
forControlState:UIControlStateNormal];
[button setAttributedTitle:title forControlState:UIControlStateNormal];
[button sizeToFit];
```
## Credits
Copyright (c) 2014 [Bradley David Bergeron](http://www.bradbergeron.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0 comments on commit 3341af1

Please sign in to comment.