diff --git a/DCRoundSwitch/DCRoundSwitch.h b/DCRoundSwitch/DCRoundSwitch.h index dd693de..d16f7a8 100644 --- a/DCRoundSwitch/DCRoundSwitch.h +++ b/DCRoundSwitch/DCRoundSwitch.h @@ -19,6 +19,7 @@ @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 diff --git a/DCRoundSwitch/DCRoundSwitch.m b/DCRoundSwitch/DCRoundSwitch.m index 8e0549b..d2b89aa 100644 --- a/DCRoundSwitch/DCRoundSwitch.m +++ b/DCRoundSwitch/DCRoundSwitch.m @@ -32,7 +32,7 @@ - (void)positionLayersAndMask; @implementation DCRoundSwitch @synthesize outlineLayer, toggleLayer, knobLayer, clipLayer, ignoreTap; @synthesize on, onText, offText; -@synthesize onTintColor; +@synthesize onTintColor, offTintColor; #pragma mark - #pragma mark Init & Memory Managment @@ -45,6 +45,7 @@ - (void)dealloc [clipLayer release]; [onTintColor release]; + [offTintColor release]; [onText release]; [offText release]; @@ -129,8 +130,9 @@ - (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.drawOffTint = NO; self.toggleLayer.clip = YES; [self.layer addSublayer:self.toggleLayer]; [self.toggleLayer setNeedsDisplay]; @@ -197,6 +199,7 @@ - (void)useLayerMasking // turn of the manual clipping (done in toggleLayer's drawInContext:) self.toggleLayer.clip = NO; self.toggleLayer.drawOnTint = YES; + self.toggleLayer.drawOffTint = YES; [self.toggleLayer setNeedsDisplay]; // create the layer mask and add that to the toggleLayer @@ -218,6 +221,7 @@ - (void)removeLayerMask // renable manual clipping (done in toggleLayer's drawInContext:) self.toggleLayer.clip = YES; self.toggleLayer.drawOnTint = self.on; + self.toggleLayer.drawOffTint = !self.on; [self.toggleLayer setNeedsDisplay]; } @@ -417,6 +421,17 @@ - (void)setOnTintColor:(UIColor *)anOnTintColor } } +- (void)setOffTintColor:(UIColor *)anOffTintColor +{ + if (anOffTintColor != offTintColor) + { + [offTintColor release]; + offTintColor = [anOffTintColor retain]; + toggleLayer.offTintColor = anOffTintColor; + [toggleLayer setNeedsDisplay]; + } +} + - (void)layoutSubviews; { CGFloat knobRadius = self.bounds.size.height + 2.0; diff --git a/DCRoundSwitch/DCRoundSwitchToggleLayer.h b/DCRoundSwitch/DCRoundSwitchToggleLayer.h index e56c631..ebfe358 100644 --- a/DCRoundSwitch/DCRoundSwitchToggleLayer.h +++ b/DCRoundSwitch/DCRoundSwitchToggleLayer.h @@ -15,12 +15,14 @@ @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 drawOffTint; @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 diff --git a/DCRoundSwitch/DCRoundSwitchToggleLayer.m b/DCRoundSwitch/DCRoundSwitchToggleLayer.m index 99e0550..3dc60b6 100644 --- a/DCRoundSwitch/DCRoundSwitchToggleLayer.m +++ b/DCRoundSwitch/DCRoundSwitchToggleLayer.m @@ -12,8 +12,8 @@ #import "DCRoundSwitchToggleLayer.h" @implementation DCRoundSwitchToggleLayer -@synthesize onString, offString, onTintColor; -@synthesize drawOnTint; +@synthesize onString, offString, onTintColor, offTintColor; +@synthesize drawOnTint, drawOffTint; @synthesize clip; @synthesize labelFont; @@ -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; @@ -63,9 +65,12 @@ - (void)drawInContext:(CGContextRef)context CGContextFillRect(context, CGRectMake(0, 0, knobCenter, self.bounds.size.height)); } - // off tint color (white) - CGContextSetFillColorWithColor(context, [UIColor colorWithWhite:0.963 alpha:1.0].CGColor); - CGContextFillRect(context, CGRectMake(knobCenter, 0, self.bounds.size.width - knobCenter, self.bounds.size.height)); + // off tint color + if (self.drawOffTint) + { + CGContextSetFillColorWithColor(context, self.offTintColor.CGColor); + CGContextFillRect(context, CGRectMake(knobCenter, 0, self.bounds.size.width - knobCenter, self.bounds.size.height)); + } // knob shadow CGContextSetShadowWithColor(context, CGSizeMake(0, 0), 1.5, [UIColor colorWithWhite:0.2 alpha:1.0].CGColor);