The effect is as follows:
ViewController.h
1 #import <UIKit/UIKit.h>23@interface viewcontroller: Uiviewcontroller4@end
Viewcontroller.m
1 #import "ViewController.h"2 #import "KMSwitch.h"3 4 @interfaceViewcontroller ()5- (void) Layoutui;6 @end7 8 @implementationViewcontroller9 Ten- (void) Viewdidload { One [Super Viewdidload]; A - [self layoutui]; - } the -- (void) didreceivememorywarning { - [Super didreceivememorywarning]; - //Dispose of any resources the can be recreated. + } - +- (void) Layoutui { A //Custom Switches 1 atKmswitch *swtcustom =[[Kmswitch alloc] initwithframe:cgrectzero]; -Cgpoint Newpoint =Self.view.center; -Swtcustom.center =Newpoint; -[Swtcustom Setleftlabeltext:@"No" - Color:[uicolor Graycolor] -Font:[uifont boldsystemfontofsize:14.0f]]; in[Swtcustom Setrightlabeltext:@"is a" - Color:[uicolor Whitecolor] toFont:[uifont boldsystemfontofsize:14.0f]]; + [Self.view Addsubview:swtcustom]; - the //Custom Switches 2 *Swtcustom =[[Kmswitch alloc] initwithframe:cgrectzero]; $Newpoint.y + = +;Panax NotoginsengSwtcustom.center =Newpoint; -[Swtcustom Setleftlabeltext:@"N" the Color:[uicolor Redcolor] +Font:[uifont boldsystemfontofsize:15.0f]]; A[Swtcustom Setrightlabeltext:@"Y" the Color:[uicolor Bluecolor] +Font:[uifont boldsystemfontofsize:15.0f]]; - [Self.view Addsubview:swtcustom]; $ } $ - @end
KMSwitch.h
1 #import<UIKit/UIKit.h>2 3 @interfaceUiswitch (Extended)4- (void) Setalternatecolors: (BOOL) Boolean;5 @end6 7 @interfaceKmswitch:uiswitch8- (void) Setleftlabeltext: (NSString *) LabelText color: (uicolor *) Labelcolor font: (Uifont *) Labelfont;9- (void) Setrightlabeltext: (NSString *) LabelText color: (uicolor *) Labelcolor font: (Uifont *) Labelfont;Ten One @end
Kmswitch.m
1 #import "KMSwitch.h"2 3 @interfaceKmswitch ()4-(UILabel *) Createlabelwithtext: (NSString *) LabelText color: (uicolor *) Labelcolor font: (Uifont *) Labelfont rect: (cgrect) rect;5 @end6 7 @implementationKmswitch8 9-(UIView *) Slider {Ten return[[Self subviews] lastobject]; One } A --(UIView *) Textholder { - return[[Self slider] subviews] [2]; the } - --(Uiimageview *) Leftview { - return[Self Textholder] subviews] [0]; + } - +-(Uiimageview *) Rightview { A return[Self Textholder] subviews] [1]; at } - -- (void) Setleftlabeltext: (NSString *) LabelText color: (uicolor *) Labelcolor font: (Uifont *) Labelfont { -Uiimageview *imgvleft =[self leftview]; -Imgvleft.image =Nil; -Imgvleft.frame = CGRectMake (0,0,0,0); in [Imgvleft addsubview:[self createlabelwithtext:labeltext - Color:labelcolor to Font:labelfont +Rect:cgrectmake ( -,5, -, -)]]; - } the *- (void) Setrightlabeltext: (NSString *) LabelText color: (uicolor *) Labelcolor font: (Uifont *) Labelfont { $Uiimageview *imgvright =[self rightview];Panax NotoginsengImgvright.image =Nil; -Imgvright.frame = CGRectMake (0,0,0,0); the [imgvright addsubview:[self createlabelwithtext:labeltext + Color:labelcolor A Font:labelfont theRect:cgrectmake (3,5, -, -)]]; + } - $-(UILabel *) Createlabelwithtext: (NSString *) LabelText color: (uicolor *) Labelcolor font: (Uifont *) Labelfont rect: (cgrect) rect { $UILabel *LBL =[[UILabel alloc] initwithframe:rect]; -Lbl.text =LabelText; -Lbl.textcolor =Labelcolor; theLbl.font =Labelfont; -Lbl.textalignment =Nstextalignmentcenter;WuyiLbl.backgroundcolor =[Uicolor Clearcolor]; the returnlbl; - } Wu - @end
067 changing Uiswitch text and color (extended knowledge: Better custom practices are implemented with inheritance UISlider, not done here)