-
-
Save seivan/cbbee880653a7fb09e78 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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