标签:
效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 @end
ViewController.m
1 #import "ViewController.h" 2 #import "KMSwitch.h" 3 4 @interface ViewController () 5 - (void)layoutUI; 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 13 [self layoutUI]; 14 } 15 16 - (void)didReceiveMemoryWarning { 17 [super didReceiveMemoryWarning]; 18 // Dispose of any resources that can be recreated. 19 } 20 21 - (void)layoutUI { 22 //自定义开关1 23 KMSwitch *swtCustom = [[KMSwitch alloc] initWithFrame:CGRectZero]; 24 CGPoint newPoint = self.view.center; 25 swtCustom.center = newPoint; 26 [swtCustom setLeftLabelText:@"否" 27 color:[UIColor grayColor] 28 font:[UIFont boldSystemFontOfSize:14.0f]]; 29 [swtCustom setRightLabelText:@"是" 30 color:[UIColor whiteColor] 31 font:[UIFont boldSystemFontOfSize:14.0f]]; 32 [self.view addSubview:swtCustom]; 33 34 //自定义开关2 35 swtCustom = [[KMSwitch alloc] initWithFrame:CGRectZero]; 36 newPoint.y += 40; 37 swtCustom.center = newPoint; 38 [swtCustom setLeftLabelText:@"N" 39 color:[UIColor redColor] 40 font:[UIFont boldSystemFontOfSize:15.0f]]; 41 [swtCustom setRightLabelText:@"Y" 42 color:[UIColor blueColor] 43 font:[UIFont boldSystemFontOfSize:15.0f]]; 44 [self.view addSubview:swtCustom]; 45 } 46 47 @end
KMSwitch.h
1 #import <UIKit/UIKit.h> 2 3 @interface UISwitch (extended) 4 - (void)setAlternateColors:(BOOL)boolean; 5 @end 6 7 @interface KMSwitch : UISwitch 8 - (void)setLeftLabelText:(NSString *)labelText color:(UIColor *)labelColor font:(UIFont *)labelFont; 9 - (void)setRightLabelText:(NSString *)labelText color:(UIColor *)labelColor font:(UIFont *)labelFont; 10 11 @end
KMSwitch.m
1 #import "KMSwitch.h" 2 3 @interface KMSwitch () 4 - (UILabel *)createLabelWithText:(NSString *)labelText color:(UIColor *)labelColor font:(UIFont *)labelFont rect:(CGRect)rect; 5 @end 6 7 @implementation KMSwitch 8 9 - (UIView *)slider { 10 return [[self subviews] lastObject]; 11 } 12 13 - (UIView *)textHolder { 14 return [[self slider] subviews][2]; 15 } 16 17 - (UIImageView *)leftView { 18 return [[self textHolder] subviews][0]; 19 } 20 21 - (UIImageView *)rightView { 22 return [[self textHolder] subviews][1]; 23 } 24 25 - (void)setLeftLabelText:(NSString *)labelText color:(UIColor *)labelColor font:(UIFont *)labelFont { 26 UIImageView *imgVLeft = [self leftView]; 27 imgVLeft.image = nil; 28 imgVLeft.frame = CGRectMake(0, 0, 0, 0); 29 [imgVLeft addSubview:[self createLabelWithText:labelText 30 color:labelColor 31 font:labelFont 32 rect:CGRectMake(28, 5, 20, 20)]]; 33 } 34 35 - (void)setRightLabelText:(NSString *)labelText color:(UIColor *)labelColor font:(UIFont *)labelFont { 36 UIImageView *imgVRight = [self rightView]; 37 imgVRight.image = nil; 38 imgVRight.frame = CGRectMake(0, 0, 0, 0); 39 [imgVRight addSubview:[self createLabelWithText:labelText 40 color:labelColor 41 font:labelFont 42 rect:CGRectMake(3, 5, 20, 20)]]; 43 } 44 45 - (UILabel *)createLabelWithText:(NSString *)labelText color:(UIColor *)labelColor font:(UIFont *)labelFont rect:(CGRect)rect { 46 UILabel *lbl = [[UILabel alloc] initWithFrame:rect]; 47 lbl.text = labelText; 48 lbl.textColor = labelColor; 49 lbl.font = labelFont; 50 lbl.textAlignment = NSTextAlignmentCenter; 51 lbl.backgroundColor = [UIColor clearColor]; 52 return lbl; 53 } 54 55 @end
067改变UISwitch文本和颜色(扩展知识:更好的自定义做法是用继承UISlider实现,这里未做)
标签:
原文地址:http://www.cnblogs.com/huangjianwu/p/4578720.html