diff --git a/DCRoundSwitch/DCRoundSwitch.h b/DCRoundSwitch/DCRoundSwitch.h index dd693de..bc6eb7b 100644 --- a/DCRoundSwitch/DCRoundSwitch.h +++ b/DCRoundSwitch/DCRoundSwitch.h @@ -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; diff --git a/DCRoundSwitch/DCRoundSwitch.m b/DCRoundSwitch/DCRoundSwitch.m index 8e0549b..e87820e 100644 --- a/DCRoundSwitch/DCRoundSwitch.m +++ b/DCRoundSwitch/DCRoundSwitch.m @@ -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 @@ -45,6 +46,7 @@ - (void)dealloc [clipLayer release]; [onTintColor release]; + [offTintColor release]; [onText release]; [offText release]; @@ -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 @@ -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]; @@ -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; diff --git a/DCRoundSwitch/DCRoundSwitchToggleLayer.h b/DCRoundSwitch/DCRoundSwitchToggleLayer.h index e56c631..3d49efe 100644 --- a/DCRoundSwitch/DCRoundSwitchToggleLayer.h +++ b/DCRoundSwitch/DCRoundSwitchToggleLayer.h @@ -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 diff --git a/DCRoundSwitch/DCRoundSwitchToggleLayer.m b/DCRoundSwitch/DCRoundSwitchToggleLayer.m index 99e0550..e4d6eb4 100644 --- a/DCRoundSwitch/DCRoundSwitchToggleLayer.m +++ b/DCRoundSwitch/DCRoundSwitchToggleLayer.m @@ -12,7 +12,7 @@ #import "DCRoundSwitchToggleLayer.h" @implementation DCRoundSwitchToggleLayer -@synthesize onString, offString, onTintColor; +@synthesize onString, offString, onTintColor, offTintColor; @synthesize drawOnTint; @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; @@ -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