Skip to content

Instantly share code, notes, and snippets.

@seivan
Forked from dawand/button objective-c
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seivan/cbbee880653a7fb09e78 to your computer and use it in GitHub Desktop.
Save seivan/cbbee880653a7fb09e78 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface KurdStyle : NSObject
// Colors
+ (UIColor*)mainColor;
+ (UIColor*)shadowColor;
// Drawing Methods
+ (void)drawBadgeButtonWithText: (NSString*)text size: (CGSize)size fontSize: (CGFloat)fontSize;
// Generated Images
+ (UIImage*)imageOfBadgeButtonWithText: (NSString*)text size: (CGSize)size fontSize: (CGFloat)fontSize;
@end
#import "KurdStyle.h"
@implementation KurdStyle
#pragma mark Cache
static UIColor* _mainColor = nil;
static UIColor* _shadowColor = nil;
#pragma mark Initialization
+ (void)initialize
{
// Colors Initialization
_mainColor = [UIColor colorWithRed: 0.306 green: 0.678 blue: 0.604 alpha: 1];
_shadowColor = [UIColor colorWithRed: 0.2 green: 0.2 blue: 0.2 alpha: 1];
}
#pragma mark Colors
+ (UIColor*)mainColor { return _mainColor; }
+ (UIColor*)shadowColor { return _shadowColor; }
#pragma mark Drawing Methods
+ (void)drawBadgeButtonWithText: (NSString*)text size: (CGSize)size fontSize: (CGFloat)fontSize
{
//// General Declarations
CGContextRef context = UIGraphicsGetCurrentContext();
//// Shadow Declarations
NSShadow* shadow = [[NSShadow alloc] init];
[shadow setShadowColor: [KurdStyle.shadowColor colorWithAlphaComponent: 0.72]];
[shadow setShadowOffset: CGSizeMake(0.1, -0.1)];
[shadow setShadowBlurRadius: 5];
//// Rectangle Drawing
CGRect rectangleRect = CGRectMake(46, 39, size.width, size.height);
UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRoundedRect: rectangleRect cornerRadius: 15];
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, shadow.shadowOffset, shadow.shadowBlurRadius, [shadow.shadowColor CGColor]);
[KurdStyle.mainColor setFill];
[rectanglePath fill];
CGContextRestoreGState(context);
NSMutableParagraphStyle* rectangleStyle = NSMutableParagraphStyle.defaultParagraphStyle.mutableCopy;
rectangleStyle.alignment = NSTextAlignmentCenter;
NSDictionary* rectangleFontAttributes = @{NSFontAttributeName: [UIFont fontWithName: @"Range-SemiBold" size: fontSize], NSForegroundColorAttributeName: UIColor.whiteColor, NSParagraphStyleAttributeName: rectangleStyle};
CGFloat rectangleTextHeight = [text boundingRectWithSize: CGSizeMake(rectangleRect.size.width, INFINITY) options: NSStringDrawingUsesLineFragmentOrigin attributes: rectangleFontAttributes context: nil].size.height;
CGContextSaveGState(context);
CGContextClipToRect(context, rectangleRect);
[text drawInRect: CGRectMake(CGRectGetMinX(rectangleRect), CGRectGetMinY(rectangleRect) + (CGRectGetHeight(rectangleRect) - rectangleTextHeight) / 2, CGRectGetWidth(rectangleRect), rectangleTextHeight) withAttributes: rectangleFontAttributes];
CGContextRestoreGState(context);
}
#pragma mark Generated Images
+ (UIImage*)imageOfBadgeButtonWithText: (NSString*)text size: (CGSize)size fontSize: (CGFloat)fontSize
{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(240, 120), NO, 0.0f);
[KurdStyle drawBadgeButtonWithText: text size: size fontSize: fontSize];
UIImage* imageOfBadgeButton = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageOfBadgeButton;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment