IOS自定义UISwitch
#import
创新互联建站是一家专注于成都网站设计、成都做网站与策划设计,海口网站建设哪家好?创新互联建站做网站,专注于网站建设10多年,网设计领域的专业建站公司;建站业务涵盖:海口等地区。海口做网站价格咨询:18980820575
// 该方法时SDK文档中没有的, 添加一个category
@interface UISwitch (extended)
- (void) setAlternateColors:(BOOL) boolean;
@end
// 自定义Slider 类
@interface _UISwitchSlider : UIView
@end
@interface UICustomSwitch : UISwitch {
}
- (void) setLeftLabelText:(NSString *)labelText
font:(UIFont*)labelFont
color: (UIColor *)labelColor;
- (void) setRightLabelText:(NSString *)labelText
font:(UIFont*)labelFont
color:(UIColor *)labelColor;
- (UILabel*) createLabelWithText:(NSString*)labelText
font:(UIFont*)labelFont
color:(UIColor*)labelColor;
@end
#import "UICustomSwitch.h"
@implementation UICustomSwitch
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect {
// Drawing code
}
- (void)dealloc {
[super dealloc];
}
- (_UISwitchSlider *) slider {
return [[self subviews] lastObject];
}
- (UIView *) textHolder {
return [[[self slider] subviews] objectAtIndex:2];
}
- (UILabel *) leftLabel {
return [[[self textHolder] subviews] objectAtIndex:0];
}
- (UILabel *) rightLabel {
return [[[self textHolder] subviews] objectAtIndex:1];
}
// 创建文本标签
- (UILabel*) createLabelWithText:(NSString*)labelText
font:(UIFont*)labelFont
color:(UIColor*)labelColor
{
CGRect rect = CGRectMake(-25.0f, -10.0f, 50.0f, 20.0f);
UILabel *label = [[UILabel alloc] initWithFrame: rect];
label.text = labelText;
label.font = labelFont;
label.textColor = labelColor;
label.textAlignment = UITextAlignmentCenter;
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 *ex) {
//
UIImageView* leftImage = (UIImageView*)[self leftLabel];
leftImage.p_w_picpath = nil;
leftImage.frame = CGRectMake(0.0f, 0.0f, 0.0f, 0.0f);
//
[leftImage addSubview: [[self createLabelWithText:labelText
font:labelFont
color:labelColor] autorelease]];
}
}
// 重新设定右边的文本
- (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 *ex) {
//
UIImageView* rightImage = (UIImageView*)[self rightLabel];
rightImage.p_w_picpath = nil;
rightImage.frame = CGRectMake(0.0f, 0.0f, 0.0f, 0.0f);
//
[rightImage addSubview: [[self createLabelWithText:labelText
font:labelFont
color:labelColor] autorelease]];
}
}
@end
文章名称:IOS自定义UISwitch
文章URL:http://scyanting.com/article/pdshej.html