iPhone SDK开发:改变UISWitch文本和颜色(当初好像我没明白,现在也没明白)

iPhone SDK中的Switch控件默认的文本为 ON OFF两种,不同的语言显示不同, 颜色均为蓝色和亮灰色,如图
如果想改变ON,OFF文本,我们必须重从UISwitch继承一个新类, 然后在新的Switch类中修改替换原有的Views, 申明代码如下
头文件
//
//  UICustomSwitch.h
//  UICatalog
//
//  Created by aish on 09-2-25.
//  Copyright 2009  .. All rights reserved.
//
 
#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

    
    
    
    
代码中添加了一个名为extended的category,主要作用是申明一下UISwitch的 setAlternateColors消息,否则在使用的时候会出现找不到该消息的警告。其实setAlternateColors已经在UISwitch中实现,只是没有在头文件中公开而已, 所以在此只是做一个申明。当调用setAlternateColors:YES 时, UISwitch的状态为“on”时会显示为橙色,否则为亮蓝色
 
实现部分
 
//
//  UICustomSwitch.m
//  UICatalog
//
//  Created by aish on 09-2-25.
//  Copyright 2009.. All rights reserved.
//
 
#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.image = 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.image = nil;
        rightImage.frame = CGRectMake(0.0f, 0.0f, 0.0f, 0.0f);
 
        //
        [rightImage addSubview: [[self createLabelWithText:labelText
                                                      font:labelFont
                                                     color:labelColor] autorelease]];   
    }
}
 
@end

      
      
      
      
实现的过程就是替换原有的标签view 以及slider。
使用也很简单,设置一下左右文本以及颜色即可,比如
 
	switchCtl = [[UICustomSwitch alloc] initWithFrame:frame];
 
//	[switchCtl setAlternateColors:YES];
	[switchCtl setLeftLabelText:@"Yes"
                            font:[UIFont boldSystemFontOfSize: 17.0f]
                           color:[UIColor whiteColor]];
    [switchCtl setRightLabelText:@"No"
                             font:[UIFont boldSystemFontOfSize: 17.0f]

你可能感兴趣的:(c,移动开发,iPhone,休闲,Object-C)