您好,登錄后才能下訂單哦!
#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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。