From 3341af1440056389de4a40f6ed57757c6ffacdab Mon Sep 17 00:00:00 2001 From: Bradley David Bergeron Date: Thu, 16 Jan 2014 09:14:10 -0500 Subject: [PATCH] Adding CocoaPods support, updating README. --- BTButton.podspec | 15 +++++++++++++ BTButton/BTButton.h | 9 +------- BTButton/BTButton.m | 36 +++++++++++++++--------------- README.md | 53 ++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 87 insertions(+), 26 deletions(-) create mode 100644 BTButton.podspec diff --git a/BTButton.podspec b/BTButton.podspec new file mode 100644 index 0000000..e9a4e26 --- /dev/null +++ b/BTButton.podspec @@ -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' => 'brad@bradbergeron.com' } + 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 diff --git a/BTButton/BTButton.h b/BTButton/BTButton.h index ffaecbf..6d79aa8 100644 --- a/BTButton/BTButton.h +++ b/BTButton/BTButton.h @@ -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 diff --git a/BTButton/BTButton.m b/BTButton/BTButton.m index 09c5efa..1727e43 100644 --- a/BTButton/BTButton.m +++ b/BTButton/BTButton.m @@ -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; @@ -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); @@ -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 diff --git a/README.md b/README.md index 6e59db5..532c451 100644 --- a/README.md +++ b/README.md @@ -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.