#import <UIKit/UIKit.h>
// 该方法时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
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。