Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I added some properties for color customization #24

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions DCRoundSwitch/DCRoundSwitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// http://domesticcat.com.au/projects
// http://github.com/domesticcatsoftware/DCRoundSwitch
//
// Modified by Jose Luis Campaña to add text and offTint color support

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
Expand All @@ -23,6 +24,13 @@
@property (nonatomic, copy) NSString *onText; // default: 'ON' - automatically localized
@property (nonatomic, copy) NSString *offText; // default: 'OFF' - automatically localized

//iZ3 Additions
@property (nonatomic, retain) UIColor *onTextColor;
@property (nonatomic, retain) UIColor *offTextColor;
@property (nonatomic, retain) UIColor *onTextShadowColor;
@property (nonatomic, retain) UIColor *offTextShadowColor;
@property (nonatomic, retain) UIColor *offTintColor;

+ (Class)knobLayerClass;
+ (Class)outlineLayerClass;
+ (Class)toggleLayerClass;
Expand Down
76 changes: 76 additions & 0 deletions DCRoundSwitch/DCRoundSwitch.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// http://domesticcat.com.au/projects
// http://github.com/domesticcatsoftware/DCRoundSwitch
//
// Modified by Jose Luis Campaña to add text and offTint color support

#import "DCRoundSwitch.h"
#import "DCRoundSwitchToggleLayer.h"
Expand All @@ -34,6 +35,13 @@ @implementation DCRoundSwitch
@synthesize on, onText, offText;
@synthesize onTintColor;

//iZ3 Additions
@synthesize onTextColor = _onTextColor;
@synthesize offTextColor = _offTextColor;
@synthesize onTextShadowColor = _onTextShadowColor;
@synthesize offTextShadowColor = _offTextShadowColor;
@synthesize offTintColor = _offTintColor;

#pragma mark -
#pragma mark Init & Memory Managment

Expand All @@ -47,6 +55,13 @@ - (void)dealloc
[onTintColor release];
[onText release];
[offText release];

//iZ3 Additions
[_onTextColor release];
[_offTextColor release];
[_offTextShadowColor release];
[_onTextShadowColor release];
[_offTintColor release];

[super dealloc];
}
Expand Down Expand Up @@ -465,4 +480,65 @@ - (void)setOffText:(NSString *)newOffText
}
}

#pragma mark iZ3 Additions
- (void)setOnTextColor:(UIColor *)onTextColor
{
if (_onTextColor != onTextColor)
{
[_onTextColor release];
_onTextColor = [onTextColor retain];

self.toggleLayer.onTextColor = onTextColor;
[self.toggleLayer setNeedsDisplay];
}
}

- (void)setOffTextColor:(UIColor *)offTextColor
{
if (_offTextColor != offTextColor)
{
[_offTextColor release];
_offTextColor = [offTextColor retain];

self.toggleLayer.offTextColor = offTextColor;
[self.toggleLayer setNeedsDisplay];
}
}

- (void)setOnTextShadowColor:(UIColor *)onTextShadowColor
{
if (_onTextShadowColor != onTextShadowColor)
{
[_onTextShadowColor release];
_onTextShadowColor = [onTextShadowColor retain];

self.toggleLayer.onTextShadowColor = onTextShadowColor;
[self.toggleLayer setNeedsDisplay];
}
}

- (void)setOffTextShadowColor:(UIColor *)offTextShadowColor
{
if (_offTextShadowColor != offTextShadowColor)
{
[_offTextShadowColor release];
_offTextShadowColor = [offTextShadowColor retain];

self.toggleLayer.offTextShadowColor = offTextShadowColor;
[self.toggleLayer setNeedsDisplay];
}
}

-(void)setOffTintColor:(UIColor *)offTintColor
{
if (_offTintColor != offTintColor)
{
[_offTintColor release];
_offTintColor = [offTintColor retain];

self.toggleLayer.offTintColor = _offTintColor;
[self.toggleLayer setNeedsDisplay];
}
}

@end
8 changes: 8 additions & 0 deletions DCRoundSwitch/DCRoundSwitchToggleLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// http://domesticcat.com.au/projects
// http://github.com/domesticcatsoftware/DCRoundSwitch
//
// Modified by Jose Luis Campaña to add text and offTint color support

#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
Expand All @@ -21,6 +22,13 @@
@property (nonatomic) BOOL drawOnTint;
@property (nonatomic) BOOL clip;

//iZ3 Additions
@property (nonatomic, retain) UIColor *onTextColor;
@property (nonatomic, retain) UIColor *offTextColor;
@property (nonatomic, retain) UIColor *onTextShadowColor;
@property (nonatomic, retain) UIColor *offTextShadowColor;
@property (nonatomic, retain) UIColor *offTintColor;

- (id)initWithOnString:(NSString *)anOnString offString:(NSString *)anOffString onTintColor:(UIColor *)anOnTintColor;

@end
72 changes: 65 additions & 7 deletions DCRoundSwitch/DCRoundSwitchToggleLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// http://domesticcat.com.au/projects
// http://github.com/domesticcatsoftware/DCRoundSwitch
//
// Modified by Jose Luis Campaña to add text and offTint color support

#import "DCRoundSwitchToggleLayer.h"

Expand All @@ -17,22 +18,78 @@ @implementation DCRoundSwitchToggleLayer
@synthesize clip;
@synthesize labelFont;

//iZ3 Additions
@synthesize onTextColor = _onTextColor;
@synthesize offTextColor = _offTextColor;
@synthesize onTextShadowColor = _onTextShadowColor;
@synthesize offTextShadowColor = _offTextShadowColor;
@synthesize offTintColor = _offTintColor;

-(void)iniController
{
//iZ3 Additions
_onTextColor = [[UIColor colorWithWhite:0.45 alpha:1.0] retain];
_offTextColor = [[UIColor whiteColor] retain];
_onTextShadowColor = [[UIColor whiteColor] retain];
_offTextShadowColor = [[UIColor colorWithWhite:0.52 alpha:1.0] retain];
_offTintColor = [[UIColor colorWithWhite:0.963 alpha:1.0] retain];
}


- (void)dealloc
{
[onString release];
[offString release];
[onTintColor release];

//iZ3
[_offTextShadowColor release];
[_offTextColor release];
[_onTextColor release];
[_onTextShadowColor release];
[_offTintColor release];


[super dealloc];
}

-(id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder])
{
[self iniController];
}
return self;
}

-(id)init
{
if ((self = [super init]))
{
[self iniController];
}
return self;
}

-(id) initWithLayer:(id)layer
{
if(self = [super initWithLayer:layer])
{
[self iniController];
}
return self;
}

- (id)initWithOnString:(NSString *)anOnString offString:(NSString *)anOffString onTintColor:(UIColor *)anOnTintColor
{
if ((self = [super init]))
{
self.onString = anOnString;
self.offString = anOffString;
self.onTintColor = anOnTintColor;

[self iniController];

}

return self;
Expand Down Expand Up @@ -64,7 +121,8 @@ - (void)drawInContext:(CGContextRef)context
}

// off tint color (white)
CGContextSetFillColorWithColor(context, [UIColor colorWithWhite:0.963 alpha:1.0].CGColor);
// CGContextSetFillColorWithColor(context, [UIColor colorWithWhite:0.963 alpha:1.0].CGColor);
CGContextSetFillColorWithColor(context, _offTintColor.CGColor);
CGContextFillRect(context, CGRectMake(knobCenter, 0, self.bounds.size.width - knobCenter, self.bounds.size.height));

// knob shadow
Expand All @@ -83,17 +141,17 @@ - (void)drawInContext:(CGContextRef)context
// 'ON' state label (self.onString)
CGSize onTextSize = [self.onString sizeWithFont:self.labelFont];
CGPoint onTextPoint = CGPointMake((textSpaceWidth - onTextSize.width) / 2.0 + knobRadius * .15, floorf((self.bounds.size.height - onTextSize.height) / 2.0) + 1.0);
[[UIColor colorWithWhite:0.45 alpha:1.0] set]; // .2 & .4
[self.onString drawAtPoint:CGPointMake(onTextPoint.x, onTextPoint.y - 1.0) withFont:self.labelFont];
[[UIColor whiteColor] set];
[_onTextShadowColor set];
[self.onString drawAtPoint:CGPointMake(onTextPoint.x, onTextPoint.y - 1.0) withFont:self.labelFont];
[_onTextColor set];
[self.onString drawAtPoint:onTextPoint withFont:self.labelFont];

// 'OFF' state label (self.offString)
CGSize offTextSize = [self.offString sizeWithFont:self.labelFont];
CGPoint offTextPoint = CGPointMake(textSpaceWidth + (textSpaceWidth - offTextSize.width) / 2.0 + knobRadius * .86, floorf((self.bounds.size.height - offTextSize.height) / 2.0) + 1.0);
[[UIColor whiteColor] set];
[self.offString drawAtPoint:CGPointMake(offTextPoint.x, offTextPoint.y + 1.0) withFont:self.labelFont];
[[UIColor colorWithWhite:0.52 alpha:1.0] set];
[_offTextShadowColor set];
[self.offString drawAtPoint:CGPointMake(offTextPoint.x, offTextPoint.y + 1.0) withFont:self.labelFont];
[_offTextColor set];
[self.offString drawAtPoint:offTextPoint withFont:self.labelFont];

UIGraphicsPopContext();
Expand Down
13 changes: 12 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,15 @@ Made available under the MIT License. Attribution would be nice.
Collaboration
-------------

If you have any feature requests/bugfixes etc. feel free to help out and send a pull request, or create a new issue.
If you have any feature requests/bugfixes etc. feel free to help out and send a pull request, or create a new issue.

iZ3 Additions
=============

More color customizations:

* onTextColor
* offTextColor
* onTextShadowColor
* offTextShadowColor
* offTintColor