您好,登錄后才能下訂單哦!
#import <UIKit/UIKit.h>
// 該方法時(shí)SDK文檔中沒有的, 添加一個(gè)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];
}
// 創(chuàng)建文本標(biāo)簽
- (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;
}
// 重新設(shè)定左邊的文本標(biāo)簽
- (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]];
}
}
// 重新設(shè)定右邊的文本
- (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
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。