IOS自定义UISwitch-创新互联
#import
// 该方法时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
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
标题名称:IOS自定义UISwitch-创新互联
网站URL:http://scyanting.com/article/iidic.html