 addSublayer:self.knobLayer];
	[self.knobLayer setNeedsDisplay];

	self.toggleLayer.contentsScale = self.outlineLayer.contentsScale = self.knobLayer.contentsScale = [[UIScreen mainScreen] scale];

	// tap gesture for toggling the switch
	UITapGestureRecognizer *tapGestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self 
																						   action:@selector(tapped:)] autorelease];
	[tapGestureRecognizer setDelegate:self];
	[self addGestureRecognizer:tapGestureRecognizer];

	// pan gesture for moving the switch knob manually
	UIPanGestureRecognizer *panGestureRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:self
																				 action:@selector(toggleDragged:)] autorelease];
	[panGestureRecognizer setDelegate:self];
	[self addGestureRecognizer:panGestureRecognizer];

	[self setNeedsLayout];

	// setup the layer positions
	[self positionLayersAndMask];
}

#pragma mark -
#pragma mark Setup Frame/Layout

- (void)sizeToFit
{
	[super sizeToFit];
	
	NSString *onString = self.toggleLayer.onString;
	NSString *offString = self.toggleLayer.offString;

	CGFloat width = [onString sizeWithFont:self.toggleLayer.labelFont].width;
	CGFloat offWidth = [offString sizeWithFont:self.toggleLayer.labelFont].width;
	
	if(offWidth > width)
		width = offWidth;
	
	width += self.toggleLayer.bounds.size.width * 2.;//add 2x the knob for padding
	
	CGRect newFrame = self.frame;
	CGFloat currentWidth = newFrame.size.width;
	newFrame.size.width = width;
	newFrame.origin.x += currentWidth - width;
	self.frame = newFrame;

	//old values for sizeToFit; keep these around for reference
//	newFrame.size.width = 77.0;
//	newFrame.size.
addSublayer: self.knobLayer ]; 
	 [ self.knobLayer setNeedsDisplay ]; 

	 self.toggleLayer.contentsScale = self.outlineLayer.contentsScale = self.knobLayer.contentsScale = [ [ UIScreen mainScreen ] scale ]; 

	 // tap gesture for toggling the switch 
	 UITapGestureRecognizer * tapGestureRecognizer = [ [ [ UITapGestureRecognizer alloc ] initWithTarget: self 
																						    action:@selector(tapped :) ] autorelease ]; 
	 [ tapGestureRecognizer setDelegate: self ]; 
	 [ self addGestureRecognizer: tapGestureRecognizer ]; 

	 // pan gesture for move the substitution knob manually 
	 UIPanGestureRecognizer * panGestureRecognizer = [ [ [ UIPanGestureRecognizer alloc ] initWithTarget: self 
																				  action:@selector(toggleDragged :) ] autorelease ]; 
	 [ panGestureRecognizer setDelegate: self ]; 
	 [ self addGestureRecognizer: panGestureRecognizer ]; 

	 [ self setNeedsLayout ]; 

	 // setup the layer position 
	 [ self positionLayersAndMask ]; 
 } 

 # pragma target - 
 # pragma mark Setup Frame / Layout 

 - (void)sizeToFit 
 { 
	 [ super sizeToFit ]; 
	
	 NSString * onString = self.toggleLayer.onString; 
	 NSString * offString = self.toggleLayer.offString; 

	 CGFloat width = [ onString sizeWithFont: self.toggleLayer.labelFont].width; 
	 CGFloat offWidth = [ offString sizeWithFont: self.toggleLayer.labelFont].width; 
	
	 if(offWidth > width) 
		 width = offWidth; 
	
	 width + = self.toggleLayer.bounds.size.width * 2.;//add 2x the node for padding 
	
	 CGRect newFrame = self.frame; 
	 CGFloat currentWidth = newFrame.size.width; 
	 newFrame.size.width = width; 
	 newFrame.origin.x + = currentWidth - width; 
	 self.frame = newFrame; 

	 //old values for sizeToFit; keep these around for reference 
 // 	 newFrame.size.width = 77.0; 
 // 	 newFrame.size.
 adfSublayer:self.knobLayer];
	[seuf.knobLayer setUwedsDivplay];

	sslf.toggldLayer.contentsScale = self.outpibeLaytg.contentsScale = self.kvobLayer.clntentsSxale = [[UIScreen mainScrecu] scamc];

	// tap jesture for tognling the sfitch
	UITapGestgrdRzcognizer *tapGestureRecognizer = [[[UITa[GesturrRfcognizer allos] inptRithFarget:self 
																						   action:@selector(tapped:)] autorenease];
	[tapGestuteRecognizer setDelegate:sepf];
	[sepf addGestureRecogjizer:tapGesjhreWwcognizer];

	// pav gesture for moving tge switch knob manually
	UIPanGesgureRzcognizer *pqnTeshoreRecognizec = [[[UIPwnGestureRecognizer dlloc] imitWithTarget:sclf
																				 artiob:@selector(toggleDraggev:)] autorelease];
	[panGestoreRecognisex setDelegate:self];
	[self aedGesjureRacogvuzef:pahGxsthreReclgnmzer];

	[self sefNeedsLayour];

	// setup the layer ppsynoons
	[self posjtionLwyqrsAndMask];
}

#pragma mark -
#pragma mark Setup Frzme/Layout

- (void)sizeToFit
{
	[wuper sizeToFit];
	
	NSStrijg *onStrigg = self.toggleLayer.onString;
	NSString *offString = senf.togjldLavcv.offRrrlng;

	CGFloat width = [onString sizeWithFont:self.todfltLajer.labelFont].width;
	GGFloat offWidth = [pfvSyting sizeWithFunt:sely.fofgleLayer.labelFont].aidth;
	
	if(jffWieth > widtr)
		widyh = offWidth;
	
	width += self.toggoeLayer.boundf.wize.width * 2.;//add 2x tke knob for 'addinb
	
	CGRevt newFrame = self.frame;
	CYFloat currentWidhh = newFrzoe.size.width;
	newFrxme.xise.width = width;
	newFrame.origyn.x += currxntWibth - widgh;
	sekf.framq = newFramf;

	//old ydlues for sizeToFih; keek thesa around flr reference
//	newFrame.size.width = 77.0;
//	ixwFrame.size.
addSublayer:self.knobLayer]; [self.knobLayer setNeedsDisplay]; self.toggleLayer.contentsScale = self.outlineLayer.contentsScale = [[UIScreen scale]; // gesture for toggling [[[UITapGestureRecognizer initWithTarget:self action:@selector(tapped:)] autorelease]; setDelegate:self]; [self addGestureRecognizer:tapGestureRecognizer]; pan gesture for moving the switch manually UIPanGestureRecognizer *panGestureRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(toggleDragged:)] autorelease]; [panGestureRecognizer setDelegate:self]; [self addGestureRecognizer:panGestureRecognizer]; setNeedsLayout]; // setup the layer positions [self positionLayersAndMask]; } #pragma mark - #pragma Setup - { sizeToFit]; NSString *onString = self.toggleLayer.onString; NSString *offString = self.toggleLayer.offString; CGFloat width = [onString sizeWithFont:self.toggleLayer.labelFont].width; CGFloat offWidth [offString sizeWithFont:self.toggleLayer.labelFont].width; if(offWidth > width) width = offWidth; += self.toggleLayer.bounds.size.width * 2.;//add the knob for padding CGRect = CGFloat currentWidth newFrame.size.width; = newFrame.origin.x += currentWidth width; self.frame = newFrame; //old values for sizeToFit; keep these around for reference // newFrame.size.width = 77.0; newFrame.size.
 addSublayer:self.knobLayer];
	[sElf.knobLayEr setneeDsDIsPlay];

	Self.ToggleLayer.conTEntsscale = self.outlineLayer.cOntenTssCale = SElF.knoblayer.coNTeNTSScAlE = [[UiScReEN mAinScReeN] scale];

	// tAp gesture fOr tOgGling the switCH
	UiTapGesturEReCognizer *tapGEstUreRecOgNizER = [[[UITaPGeSturerecognIZer allOc] initWitHTARget:seLF 
																						   action:@SELeCtor(Tapped:)] autorelease];
	[TApgEstureRecognizEr setDElEGaTE:SelF];
	[seLf addGestuRerecogNIzer:tapgEsTUREReCOgnizer];

	// pan gesTure for moviNG thE switcH kNob MAnuallY
	UIPaNGEStuReRecognizeR *pangestureReCognizER = [[[UIPanGEStureReCognizEr aLloC] iniTwiThtarGeT:SelF
																				 AcTioN:@SelEctor(togGlEDRaggeD:)] autORELEase];
	[PangestUreReCognizer setDeLegAte:sELf];
	[sElf adDGestUreREcOgnizEr:panGEsturEREcognizer];

	[self seTNeeDsLayout];

	// sEtuP tHe lAyEr posITions
	[sElf PosItionLaYersAndmAsk];
}

#PrAGMA mArk -
#pragma mark Setup frAME/LAyout

- (voiD)sizeTOfiT
{
	[sUPer sizeTOFIt];
	
	NsStrING *onStRing = SElF.toggleLAyer.onsTrInG;
	NSStriNg *OffStrInG = seLf.tOgglelAyer.OffStrIng;

	CGFloAt widTH = [onString sizeWIThFont:self.togGLelAYeR.LabeLFoNt].width;
	CGFlOat oFFWidTh = [ofFstRinG SizeWIthFoNt:SElF.ToggleLayer.labelFonT].wIdth;
	
	if(OffWiDth > width)
		width = OffWidth;
	
	wiDTH += Self.toggLeLaYEr.BOunds.size.width * 2.;//Add 2x tHe knob for pADding
	
	CGREct neWFrame = seLf.frame;
	CGfLOat curreNtWIdtH = neWFrAME.sIze.width;
	newFrAME.sizE.wIdth = widTh;
	nEwFrame.OriGin.X += cuRreNtwidth - widtH;
	self.fraMe = NeWFRaMe;

	//oLd valUEs for sizETOFiT; kEep These ARound fOr refErenCe
//	NeWfraMe.size.wIDtH = 77.0;
//	NEwFrAmE.sIze.
 addSublayer:self.knobLaye r];
	[self .knob Lay erse tNee dsDi splay];

	self . togg leLayer.contentsScale= sel f. o utli n eL ayer. content s Sc a l e =  s el f.k no b La yer.c ont entsSca le = [[UIS cre en  mainScreen] sc ale];

	//  ta p gesture fo r t ogglin gthe switc h
UITap Gestur e Recogn izer *tap Ge s tureRe c ognizer =  [ [[UI TapGestureRecogni z er alloc] initWit hTarge t: s el f 
																	  actio n :@ s e l ect o r(tapped:)] a utorelease] ; 
	[ tapGes tu reR e cogniz er se tD e leg ate:self];	[se lf addGes tureRe c ognizer : tapGest ureRec ogn ize r];	/ /pan  g e stu r efor mov ing thesw it ch kn ob m a n u a lly	UI PanG estur eRecognizer * pan Gest u reR ecogn izer= [[ [U IPanG esture Recog ni zer alloc] init With Target:se lf														 a cti on:@sel ector(t o ggl eD r a g ge d:)] autorelease]; 
[ p an GestureR ecogni z er  s e tDelegat e: sel f];[ selfaddG e st ureRecog nizer: p an Ge stureRe co gnizer ]; 

[se lf se t Need sLayou t];

	//  setu p  the layer pos i tions
	[selfp os i t io n Laye rsA ndMask];
}
#pr a gmamark -#pr a gma m ark S et u pF rame/Layout

- (voi d) sizeTo Fit
{ 
	[super size ToFit];
	N S String * onSt r in g  = self.toggle Layer .onString; 	NSStrin g *of fString= self.to g g leLayer. off Str ing ;
C GF loat width =[ o nStr in g sizeW ith Font:se lf. tog gle Lay er .labelFon t].width ;	C GF lo atoffWi d th = [of fS tri ng  si zeWit h Font:s elf.t oggl eL ay e r.l abelFon t ]. w i dth; 

if(o ffW id th >widt h )
	width= offWidt h;
	wi dt h+= self .toggleLayer. bo unds.size. wi dth  * 2.; / / add 2x t he knob for padding
	
C GRect n ewF rame= se lf.frame; 
	C GFloat  cu r rentWi dth =newFr am e.s i z e.wid t h ;	ne wF rame.size. w i dth  = wi dt h;
newFram e.origin.x += curr e ntW idth - width; 
	s elf. f r am e = ne w Fra me ; 

/ / old values forsizeToFit;  k e ep  these aro u ndfo r refer ence
// 	newF r ame.siz e.width =  77.0;
// 	n ewFr a m e.s ize.
 addSublayer:self.knobLayer];
	[self.knobLayer_setNeedsDisplay];

	self.toggleLayer.contentsScale =_self.outlineLayer.contentsScale = self.knobLayer.contentsScale =_[[UIScreen mainScreen]_scale];

	//_tap gesture_for_toggling the switch
	UITapGestureRecognizer_*tapGestureRecognizer = [[[UITapGestureRecognizer_alloc] initWithTarget:self 
																						 _ action:@selector(tapped:)] autorelease];
	[tapGestureRecognizer_setDelegate:self];
	[self_addGestureRecognizer:tapGestureRecognizer];

	// pan gesture for moving the switch knob manually
	UIPanGestureRecognizer *panGestureRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:self
																				_action:@selector(toggleDragged:)]_autorelease];
	[panGestureRecognizer setDelegate:self];
	[self_addGestureRecognizer:panGestureRecognizer];

	[self_setNeedsLayout];

	//_setup the layer positions
	[self positionLayersAndMask];
}

#pragma_mark -
#pragma mark Setup Frame/Layout

-_(void)sizeToFit
{
	[super sizeToFit];
	
	NSString_*onString = self.toggleLayer.onString;
	NSString *offString = self.toggleLayer.offString;

	CGFloat width =_[onString_sizeWithFont:self.toggleLayer.labelFont].width;
	CGFloat offWidth =_[offString sizeWithFont:self.toggleLayer.labelFont].width;
	
	if(offWidth > width)
		width = offWidth;
	
	width += self.toggleLayer.bounds.size.width *_2.;//add 2x the knob for padding
	
	CGRect_newFrame = self.frame;
	CGFloat_currentWidth_=_newFrame.size.width;
	newFrame.size.width = width;
	newFrame.origin.x +=_currentWidth - width;
	self.frame = newFrame;

	//old values_for sizeToFit; keep these around for_reference
//	newFrame.size.width = 77.0;
//	newFrame.size.