Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

label font, color and shadow #18

Open
wants to merge 2 commits into from

4 participants

@anujbheda

Change certain label attributes.

Usage:
self.switch.labelColor = [UIColor blackColor];
self.switch.labelShadowColor = [UIColor grayColor];
self.switch.labelFont = [UIFont systemFontOfSize:18.0f];

@lucianf

Pretty useful, 1+ vote for merging this.

@sidwyn

+1 to merge this too. Really useful.

@623HS

+1 for the merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 2, 2012
  1. @anujbheda
  2. @anujbheda

    dealloc

    anujbheda authored
This page is out of date. Refresh to see the latest.
View
3  DCRoundSwitch/DCRoundSwitch.h
@@ -22,6 +22,9 @@
@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, retain) UIFont *labelFont;
+@property (nonatomic, retain) UIColor *labelColor;
+@property (nonatomic, retain) UIColor *labelShadowColor;
+ (Class)knobLayerClass;
+ (Class)outlineLayerClass;
View
38 DCRoundSwitch/DCRoundSwitch.m
@@ -33,6 +33,7 @@ @implementation DCRoundSwitch
@synthesize outlineLayer, toggleLayer, knobLayer, clipLayer, ignoreTap;
@synthesize on, onText, offText;
@synthesize onTintColor;
+@synthesize labelFont, labelColor, labelShadowColor;
#pragma mark -
#pragma mark Init & Memory Managment
@@ -47,6 +48,10 @@ - (void)dealloc
[onTintColor release];
[onText release];
[offText release];
+
+ [labelFont release];
+ [labelColor release];
+ [labelShadowColor release];
[super dealloc];
}
@@ -465,4 +470,37 @@ - (void)setOffText:(NSString *)newOffText
}
}
+- (void)setLabelFont:(UIFont *)newLabelFont
+{
+ if (newLabelFont != labelFont)
+ {
+ [labelFont release];
+ labelFont = [newLabelFont retain];
+ self.toggleLayer.labelFont = labelFont;
+ [self.toggleLayer setNeedsDisplay];
+ }
+}
+
+- (void)setLabelColor:(UIColor *)newLabelColor
+{
+ if (newLabelColor != labelColor)
+ {
+ [labelColor release];
+ labelColor = [newLabelColor retain];
+ self.toggleLayer.labelColor = labelColor;
+ [self.toggleLayer setNeedsDisplay];
+ }
+}
+
+- (void)setLabelShadowColor:(UIColor *)newLabelShadowColor
+{
+ if (newLabelShadowColor != labelShadowColor)
+ {
+ [labelShadowColor release];
+ labelShadowColor = [newLabelShadowColor retain];
+ self.toggleLayer.labelShadowColor = labelShadowColor;
+ [self.toggleLayer setNeedsDisplay];
+ }
+}
+
@end
View
4 DCRoundSwitch/DCRoundSwitchToggleLayer.h
@@ -17,7 +17,9 @@
@property (nonatomic, retain) UIColor *onTintColor;
@property (nonatomic, retain) NSString *onString;
@property (nonatomic, retain) NSString *offString;
-@property (nonatomic, readonly) UIFont *labelFont;
+@property (nonatomic, retain) UIFont *labelFont;
+@property (nonatomic, retain) UIColor *labelColor;
+@property (nonatomic, retain) UIColor *labelShadowColor;
@property (nonatomic) BOOL drawOnTint;
@property (nonatomic) BOOL clip;
View
31 DCRoundSwitch/DCRoundSwitchToggleLayer.m
@@ -15,13 +15,17 @@ @implementation DCRoundSwitchToggleLayer
@synthesize onString, offString, onTintColor;
@synthesize drawOnTint;
@synthesize clip;
-@synthesize labelFont;
+@synthesize labelFont, labelColor, labelShadowColor;
- (void)dealloc
{
[onString release];
[offString release];
[onTintColor release];
+
+ [labelFont release];
+ [labelColor release];
+ [labelShadowColor release];
[super dealloc];
}
@@ -40,7 +44,10 @@ - (id)initWithOnString:(NSString *)anOnString offString:(NSString *)anOffString
- (UIFont *)labelFont
{
- return [UIFont boldSystemFontOfSize:ceilf(self.bounds.size.height * .6)];
+ if (labelFont != nil)
+ return labelFont;
+ else
+ return [UIFont boldSystemFontOfSize:ceilf(self.bounds.size.height * .6)];
}
- (void)drawInContext:(CGContextRef)context
@@ -83,17 +90,29 @@ - (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
+ if (labelShadowColor)
+ [labelShadowColor set];
+ else
+ [[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];
+ if (labelColor)
+ [labelColor set];
+ else
+ [[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];
+ if (labelShadowColor)
+ [labelShadowColor set];
+ else
+ [[UIColor whiteColor] set];
[self.offString drawAtPoint:CGPointMake(offTextPoint.x, offTextPoint.y + 1.0) withFont:self.labelFont];
- [[UIColor colorWithWhite:0.52 alpha:1.0] set];
+ if (labelColor)
+ [labelColor set];
+ else
+ [[UIColor colorWithWhite:0.52 alpha:1.0] set];
[self.offString drawAtPoint:offTextPoint withFont:self.labelFont];
UIGraphicsPopContext();
Something went wrong with that request. Please try again.