Uiswitch Adding tags

Source: Internet
Author: User

Add a label to Uiswitch. The switch label content appears when sliding left or right.

Code:

////uiswitch+jglabel.h//Jgswitch////Created by SL on 15/4/11.//Copyright (c) 2015 Madordie. All rights reserved.//////Description://1. Add a switch label to the Uiswitch//2. You can adjust the relevant properties of the label as needed//3. Category implementation, without changing the code to add//////Code Source:http://www.cnblogs.com/madordie/////Ideas://1. Find the corresponding view to add two labels//#import<UIKit/UIKit.h>@interfaceUiswitch (Jglabel)/** * Set the switch title text * 1. Set the font content, size, font color and other attributes as you want. * 2. It is not recommended to set the background color here, if necessary, to invoke the parent class method settings. * 3. Adjust the label frame manually as needed to make your label more aesthetically pleasing. */#pragmaMark-title-(UILabel *) Ontitle;-(UILabel *) Offtitle;@end/** * NOTE: * 1.offTitle alignment is the default right alignment. * 2.onTitle Default Left Alignment * 3. You got it?*///Example code:/*//initialization properties, IB custom Uiswitch *myswitch = [[Uiswitch alloc] init];     Myswitch.center = Self.view.center;     Myswitch.transform = Cgaffinetransformmakescale (2.0f, 2.0f);          [Self.view Addsubview:myswitch];     Set the Caption property (used as Uilabel) MySwitch.offTitle.text = @ "Off";     MySwitch.onTitle.text [email protected] "on";     MySwitch.offTitle.text = @ "Off";      MySwitch.onTitle.text [email protected] "on"; */

////UISWITCH+JGLABEL.M//Jgswitch////Created by SL on 15/4/11.//Copyright (c) 2015 Madordie. All rights reserved.//#import "uiswitch+jglabel.h"#import<objc/runtime.h>Static Const void*ontitlekey = &Ontitlekey;Static Const void*offtitlekey = &Offtitlekey;#defineJggetontitle Objc_getassociatedobject (self, Ontitlekey)#defineJggetofftitle Objc_getassociatedobject (self, Offtitlekey)#defineJgsetontitle (JLabel) objc_setassociatedobject (self, ontitlekey, JLabel, objc_association_retain_nonatomic)#defineJgsetofftitle (JLabel) objc_setassociatedobject (self, offtitlekey, JLabel, objc_association_retain_nonatomic)@implementationUiswitch (Jglabel)#pragmamark-getter-(UILabel *) Ontitle {UILabel*_ontitle =Jggetontitle; if(_ontitle) {return_ontitle; } _ontitle=[self jgmakelabel]; [Self.subviews Enumerateobjectsusingblock:^(IDobj, Nsuinteger idx, BOOL *stop) {UIView*view =obj; if([View iskindofclass:nsclassfromstring (@"_uiswitchinternalviewneuestyle1")] {[View.subviews enumerateobjectsusingblock:^(IDobj, Nsuinteger idx, BOOL *stop) {UIView*aview =obj; if([Aview Iskindofclass:[uiimageviewclass]]) {[view insertsubview:_ontitle Belowsubview:aview];        }            }];    }    }];    Jgsetontitle (_ontitle); return_ontitle;}-(UILabel *) Offtitle {UILabel*_offtitle =Jggetofftitle; if(_offtitle) {return_offtitle; } _offtitle=[self jgmakelabel]; [Self insertsubview:_offtitle atindex:0];    [_offtitle Settextalignment:nstextalignmentright];    Jgsetofftitle (_offtitle); return_offtitle;}#pragmamark-tools-(UILabel *) Jgmakelabel {UILabel*label =[[UILabel alloc] init];    [Label Setbackgroundcolor:[uicolor Clearcolor]; Label.textcolor=[Uicolor Blackcolor]; CGRect Frame=Self.bounds; Frame.origin.x=4; Frame.size.width-=8; Label.frame=frame; returnlabel;}@end
UISWITCH+JGLABEL.M

Http://www.cnblogs.com/madordie/p/4416111.html

Uiswitch Adding tags

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.