Objective-C Learning Notes - UISwitch (Rewrite)(坑)
重写 UISwitch 类以添加修改其文本信息的方法:
好讨厌一本书里面的样例程序各个编写习惯都不一样....
// UISwitchCustom.h #import <UIKit/UIKit.h> /** 添加一个名为 extended 的 category, 声明一下 UISwitch 的 setAlertnateColors 消息,否则在使用的时候会出现找不到该消息的警告.其实 setAlertnateColors 已经在 UISwitch 中实现,只是没u 头文件中公开而已,所以在此做一个声明... 当调用setAlertnateColors:YES 时,UISwitch 的状态为"on"时显示为橙色. **/ @interface UISwitch (extended) - (void) setAlertnateColors:(BOOL)boolean; @end //自定义 slider 类,方便存储数据而已(好像 wwwww @interface _UISwitchSlider : UISlider @end //自定义 UISwitch 类并拓展可以修改按钮上面的文字方法 @interface UISwitchCustom : UISwitch //设置左边的文字 - (void) setLeftLabelText:(NSString *)labelText font:(UIFont *)labelFont color:(UIColor *)labelColor; //设置右边的文字 - (void) setRightLabelText:(NSString *)labelText font:(UIFont *)labelFont color:(UIColor *)labelColor; //一个方便创建 Label 的方法而已,其实它不应该出现在这里的吧... - (UILabel *)createLabelWithText:(NSString *)labelText font:(UIFont *)labelFont color:(UIColor *)labelColor; @end
// UISwitchCustom.m #import "UISwitchCustom.h" @implementation UISwitchCustom -(id)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]){ //初始化 } return self; } -(void)drawRect:(CGRect)rect{ //绘图函数(啥? } //坑爹做法,为啥不直接定义一个结构? -(_UISwitchSlider *)slider{ return [[self subviews] lastObject]; } //为啥要这样写啊混蛋! -(UILabel *)textHolder{ return [[[self slider]subviews]objectAtIndex:2]; } //.. -(UILabel *)leftLabel{ return [[[self slider]subviews]objectAtIndex:0]; } //.. -(UILabel*)rightLabel{ return [[[self slider]subviews]objectAtIndex:1]; } //创建文本标签 -(UILabel*)createLabelWithText:(NSString *)labelText font:(UIFont *)labelFont color:(UIColor *)labelColor{ //这个大小不是应该根据自身的 UISwitch 大小来调么... CGRect rect = CGRectMake(-25.0f, -10.0f, 50.0f, 20.0f); //使用以上的 Rect 创建 UILabel 实例 UILabel *label = [[UILabel alloc]initWithFrame:rect]; label.text = labelText; label.font = labelFont; label.textColor = labelColor; label.textAlignment = NSTextAlignmentCenter; label.backgroundColor = [UIColor clearColor]; return label; } //重新设定左边的文本标签 -(void)setLeftLabelText:(NSString *)labelText font:(UIFont *)labelFont color:(UIColor *)labelColor{ @try { // [[self leftLabel]setText:labelText]; [[self leftLabel]setFont:labelFont]; [[self leftLabel]setTextColor:labelColor]; } @catch (NSException *exception) { // UIImageView *leftImage = (UIImageView *)[self leftLabel]; leftImage.image = nil; leftImage.frame = CGRectMake(0.0f, 0.0f, 0.0f, 0.0f); [leftImage addSubview:[self createLabelWithText:labelText font:labelFont color:labelColor]]; } @finally { } } //重新设定右边的文本标签 -(void)setRightLabelText:(NSString *)labelText font:(UIFont *)labelFont color:(UIColor *)labelColor{ @try { // [[self rightLabel]setText:labelText]; [[self rightLabel]setFont:labelFont]; [[self rightLabel]setTextColor:labelColor]; } @catch (NSException *exception) { // UIImageView *rightImage = (UIImageView *)[self rightLabel]; rightImage.image = nil; rightImage.frame = CGRectMake(0.0f, 0.0f, 0.0f, 0.0f); [rightImage addSubview:[self createLabelWithText:labelText font:labelFont color:labelColor]]; } @finally { } } @end
调用(失败了 想哭不会调试 orz
#import "ViewController.h" #import "UISwitchCustom.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UISwitchCustom *mySwitch = [[UISwitchCustom alloc]init]; [mySwitch setLeftLabelText:@"YESS" font:[UIFont systemFontOfSize:17] color:[UIColor redColor]]; [mySwitch setRightLabelText:@"NOOP" font:[UIFont systemFontOfSize:17] color:[UIColor grayColor]]; [self.view addSubview:mySwitch]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end