Custom UISwitch for iphone

Source: Internet
Author: User

You can modify the UISwitch title and customize the UISwitch using either of the following methods:

1. Use category extension UISwitch.

As follows:

The following is a UISwitch. h file:

 

# Import <UIKit/UIKit. h>

 

@ Interface UISwitch (tagged)

+ (UISwitch *) switchWithLeftText: (NSString *) tag1 andRight: (NSString *) tag2;

@ Property (nonatomic, readonly) UILabel * label1;

@ Property (nonatomic, readonly) UILabel * label2;

@ End

 

UISwitch. m file:

# Import UISwitch-Extended.h"

 

# Define TAG_OFFSET 900

 

@ Implementation UISwitch (tagged)

-(Void) spelunkAndTag: (UIView *) aView withCount :( int *) count

{

For (UIView * subview in [aView subviews])

{

If ([subview isKindOfClass: [UILabel class])

{

* Count + = 1;

[Subview setTag TAG_OFFSET + * count)];

}

Else

[Self spelunkAndTag: subview withCount: count];

}

}

 

-(UILabel *) label1

{

Return (UILabel *) [self viewWithTag: TAG_OFFSET + 1];

}

 

-(UILabel *) label2

{

Return (UILabel *) [self viewWithTag: TAG_OFFSET + 2];

}

 

+ (UISwitch *) switchWithLeftText: (NSString *) tag1 andRight: (NSString *) tag2

{

UISwitch * switchView = [[UISwitch alloc] initWithFrame: CGRectZero];

 

Int labelCount = 0;

[SwitchView spelunkAndTag: switchView withCount: & labelCount];

 

If (labelCount = 2)

{

[SwitchView. label1 setText: tag1];

[SwitchView. label2 setText: tag2];

}

 

Return [switchView autorelease];

}

 

@ End

 

2. There is another method, which is relatively simple but difficult to understand.

 


 

 

UISwitch * isFooOrBar = [[UISwitch alloc] init];

 

 

 


(UILabel *) [[[[[isFooOrBar subviews] lastObject] subviews] objectAtIndex: 2] subviews] objectAtIndex: 0]). text = @ "Foo ";

(UILabel *) [[[[[isFooOrBar subviews] lastObject] subviews] objectAtIndex: 2] subviews] objectAtIndex: 1]). text = @ "Bar ";

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.