067 changing Uiswitch text and color (extended knowledge: Better custom practices are implemented with inheritance UISlider, not done here)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.