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

Optionally use images for On or Off states #8

Open
wants to merge 2 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
6 changes: 4 additions & 2 deletions DCRoundSwitch/DCRoundSwitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@

@property (nonatomic, retain) UIColor *onTintColor; // default: blue (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 *onText; // default: 'ON' - not automatically localized!
@property (nonatomic, copy) NSString *offText; // default: 'OFF' - not automatically localized!
@property (nonatomic, copy) UIImage *onImage;
@property (nonatomic, copy) UIImage *offImage;

+ (Class)knobLayerClass;
+ (Class)outlineLayerClass;
Expand Down
26 changes: 25 additions & 1 deletion DCRoundSwitch/DCRoundSwitch.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ - (void)positionLayersAndMask;

@implementation DCRoundSwitch
@synthesize outlineLayer, toggleLayer, knobLayer, clipLayer, ignoreTap;
@synthesize on, onText, offText;
@synthesize on, onText, offText, onImage, offImage;
@synthesize onTintColor;

#pragma mark -
Expand All @@ -47,6 +47,8 @@ - (void)dealloc
[onTintColor release];
[onText release];
[offText release];
[onImage release];
[offImage release];

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

- (void)setOnImage:(UIImage *)newOnImage
{
if (newOnImage != onImage)
{
[onImage release];
onImage = [newOnImage copy];
toggleLayer.onImage = onImage;
[toggleLayer setNeedsDisplay];
}
}

- (void)setOffImage:(UIImage *)newOffImage
{
if (newOffImage != offImage)
{
[offImage release];
offImage = [newOffImage copy];
toggleLayer.offImage = offImage;
[toggleLayer setNeedsDisplay];
}
}

@end
3 changes: 3 additions & 0 deletions DCRoundSwitch/DCRoundSwitchToggleLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
@property (nonatomic, retain) UIColor *onTintColor;
@property (nonatomic, retain) NSString *onString;
@property (nonatomic, retain) NSString *offString;
@property (nonatomic, retain) UIImage *onImage;
@property (nonatomic, retain) UIImage *offImage;
@property (nonatomic, readonly) UIFont *labelFont;
@property (nonatomic) BOOL drawOnTint;
@property (nonatomic) BOOL clip;

- (id)initWithOnString:(NSString *)anOnString offString:(NSString *)anOffString onTintColor:(UIColor *)anOnTintColor;
- (id)initWithOnImage:(UIImage *)anOnImage offImage:(UIImage *)anOffImage onTintColor:(UIColor *)anOnTintColor;

@end
64 changes: 46 additions & 18 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, onImage, offImage, onTintColor;
@synthesize drawOnTint;
@synthesize clip;
@synthesize labelFont;
Expand All @@ -22,6 +22,8 @@ - (void)dealloc
[onString release];
[offString release];
[onTintColor release];
[onImage release];
[offImage release];

[super dealloc];
}
Expand All @@ -33,11 +35,25 @@ - (id)initWithOnString:(NSString *)anOnString offString:(NSString *)anOffString
self.onString = anOnString;
self.offString = anOffString;
self.onTintColor = anOnTintColor;
self.onImage = nil;
self.offImage = nil;
}

return self;
}

- (id)initWithOnImage:(UIImage *)anOnImage offImage:(UIImage *)anOffImage onTintColor:(UIColor *)anOnTintColor
{
if ((self = [super init]))
{
self.onImage = anOnImage;
self.offImage = anOffImage;
self.onTintColor = anOnTintColor;
}

return self;
}

- (UIFont *)labelFont
{
return [UIFont boldSystemFontOfSize:ceilf(self.bounds.size.height * .6)];
Expand Down Expand Up @@ -79,23 +95,35 @@ - (void)drawInContext:(CGContextRef)context
CGFloat textSpaceWidth = (self.bounds.size.width / 2) - (knobRadius / 2);

UIGraphicsPushContext(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];
[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];
[self.offString drawAtPoint:offTextPoint withFont:self.labelFont];

//withOffset:drawPercent * (boundsRect.size.width - knobWidth)
//inTrackWidth:(boundsRect.size.width - knobWidth)];

if (self.onImage) {
CGPoint onImagePoint = CGPointMake((textSpaceWidth - self.onImage.size.width) / 2.0 + knobRadius * .15, floorf((self.bounds.size.height - self.onImage.size.height) / 2.0) + 1.0);
[self.onImage drawAtPoint:onImagePoint];
} else {
// '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];
[self.onString drawAtPoint:onTextPoint withFont:self.labelFont];
}

if (self.offImage) {
CGPoint offImagePoint = CGPointMake(textSpaceWidth + (textSpaceWidth - self.onImage.size.width) / 2.0 + knobRadius * .86, floorf((self.bounds.size.height - self.onImage.size.height) / 2.0) + 1.0);
[offImage drawAtPoint:offImagePoint];
} else {
// '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];
[self.offString drawAtPoint:offTextPoint withFont:self.labelFont];
}

UIGraphicsPopContext();
}

Expand Down