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 ";