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

Added "off" tint color property #20

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
2 changes: 2 additions & 0 deletions DCRoundSwitch/DCRoundSwitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
@interface DCRoundSwitch : UIControl

@property (nonatomic, retain) UIColor *onTintColor; // default: blue (matches normal UISwitch)
@property (nonatomic, retain) UIColor *offTintColor; // default: white (matches normal UISwitch)
@property (nonatomic, getter=isOn) BOOL on; // default: NO
@property (nonatomic, copy) NSString *onText; // default: 'ON' - automatically localized
@property (nonatomic, copy) NSString *offText; // default: 'OFF' - automatically localized
@property (nonatomic, copy) NSString *userID; // default: 'OFF' - automatically localized

+ (Class)knobLayerClass;
+ (Class)outlineLayerClass;
Expand Down
21 changes: 17 additions & 4 deletions DCRoundSwitch/DCRoundSwitch.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ - (void)positionLayersAndMask;
@implementation DCRoundSwitch
@synthesize outlineLayer, toggleLayer, knobLayer, clipLayer, ignoreTap;
@synthesize on, onText, offText;
@synthesize onTintColor;
@synthesize onTintColor, offTintColor;
@synthesize userID;

#pragma mark -
#pragma mark Init & Memory Managment
Expand All @@ -45,6 +46,7 @@ - (void)dealloc
[clipLayer release];

[onTintColor release];
[offTintColor release];
[onText release];
[offText release];

Expand Down Expand Up @@ -115,7 +117,7 @@ - (void)setup
// the switch has three layers, (ordered from bottom to top):
//
// * toggleLayer * (bottom of the layer stack)
// this layer contains the onTintColor (blue by default), the text, and the shadown for the knob. the knob shadow is
// this layer contains the onTintColor (blue by default), and the offTintColor (off-white by default), the text, and the shadown for the knob. the knob shadow is
// on this layer because it needs to go under the outlineLayer so it doesn't bleed out over the edge of the control.
// this layer moves when the switch moves

Expand All @@ -129,7 +131,7 @@ - (void)setup
// this is the knob, and sits on top of the layer stack. note that the knob shadow is NOT drawn here, it is drawn on the
// toggleLayer so it doesn't bleed out over the outlineLayer.

self.toggleLayer = [[[[[self class] toggleLayerClass] alloc] initWithOnString:self.onText offString:self.offText onTintColor:[UIColor colorWithRed:0.000 green:0.478 blue:0.882 alpha:1.0]] autorelease];
self.toggleLayer = [[[[[self class] toggleLayerClass] alloc] initWithOnString:self.onText offString:self.offText onTintColor:[UIColor colorWithRed:0.000 green:0.478 blue:0.882 alpha:1.0] offTintColor:[UIColor colorWithWhite:0.963 alpha:1.0]] autorelease];
self.toggleLayer.drawOnTint = NO;
self.toggleLayer.clip = YES;
[self.layer addSublayer:self.toggleLayer];
Expand Down Expand Up @@ -417,10 +419,21 @@ - (void)setOnTintColor:(UIColor *)anOnTintColor
}
}

- (void)setOffTintColor:(UIColor *)anOffTintColor
{
if (anOffTintColor != offTintColor)
{
[offTintColor release];
offTintColor = [anOffTintColor retain];
self.toggleLayer.offTintColor = anOffTintColor;
[self.toggleLayer setNeedsDisplay];
}
}

- (void)layoutSubviews;
{
CGFloat knobRadius = self.bounds.size.height + 2.0;
self.knobLayer.frame = CGRectMake(0, 0, knobRadius, knobRadius);
self.knobLayer.frame = CGRectMake(0, 0, knobRadius - 2.0, knobRadius);
CGSize toggleSize = CGSizeMake(self.bounds.size.width * 2 - (knobRadius - 4), self.bounds.size.height);
CGFloat minToggleX = -toggleSize.width / 2.0 + knobRadius / 2.0 - 1;
CGFloat maxToggleX = -1;
Expand Down
3 changes: 2 additions & 1 deletion DCRoundSwitch/DCRoundSwitchToggleLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
@interface DCRoundSwitchToggleLayer : CALayer

@property (nonatomic, retain) UIColor *onTintColor;
@property (nonatomic, retain) UIColor *offTintColor;
@property (nonatomic, retain) NSString *onString;
@property (nonatomic, retain) NSString *offString;
@property (nonatomic, readonly) UIFont *labelFont;
@property (nonatomic) BOOL drawOnTint;
@property (nonatomic) BOOL clip;

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

@end
8 changes: 5 additions & 3 deletions DCRoundSwitch/DCRoundSwitchToggleLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#import "DCRoundSwitchToggleLayer.h"

@implementation DCRoundSwitchToggleLayer
@synthesize onString, offString, onTintColor;
@synthesize onString, offString, onTintColor, offTintColor;
@synthesize drawOnTint;
@synthesize clip;
@synthesize labelFont;
Expand All @@ -22,17 +22,19 @@ - (void)dealloc
[onString release];
[offString release];
[onTintColor release];
[offTintColor release];

[super dealloc];
}

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

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

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

// knob shadow
Expand Down