Skip to content

Instantly share code, notes, and snippets.

@breeno
Created February 1, 2012 21:34
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save breeno/1719605 to your computer and use it in GitHub Desktop.
#import <UIKit/UIKit.h>
@interface UIButton (UIButton_BackgroundImageAdditions)
-(void) setBackgroundImageWithColor: (UIColor *) color
cornerRadius: (CGFloat) cornerRadius
forState: (UIControlState) state;
@end
#import "UIButton+BackgroundImageAdditions.h"
@implementation UIButton (UIButton_BackgroundImageAdditions)
-(void) setBackgroundImageWithColor: (UIColor *) color
cornerRadius: (CGFloat) cornerRadius
forState: (UIControlState) state
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
UIBezierPath *roundedRectPath = [UIBezierPath bezierPathWithRoundedRect: self.bounds byRoundingCorners: UIRectCornerAllCorners cornerRadii: CGSizeMake(cornerRadius, cornerRadius)];
CGContextAddPath( context, roundedRectPath.CGPath);
CGContextClip(context);
[color setFill];
UIRectFill(self.bounds);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
CGContextRestoreGState(context);
UIGraphicsEndImageContext();
[self setBackgroundImage: image forState: state];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment