关于一个简单的复选框的定制

来源:互联网 发布:淘宝联盟网 编辑:程序博客网 时间:2024/05/22 13:22

代码如下:

/**

 * 使用方法:

 * // 创建复选框

 * - (void)createCheckBox

 * {

 * WSYCheckBox *checkBox = [[WSYCheckBox alloc] initWithFrame:CGRectMake(60, 160,100,30)];

 * [checkBox setCheckBoxText:@"点击试试看"];

 * checkBox.backgroundColor = [UIColor yellowColor];

 * checkBox.tag = 100;

 * [self.view addSubview:checkBox];

 * // 创建点击事件

 * UIControl *c = [[UIControl alloc] initWithFrame:checkBox.frame];

 * [c addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];

 * [self.view addSubview:c];

 * }

 

 * // 给复选框添加点击事件

 * - (void)click

 * {

 * WSYCheckBox *checkBox = (WSYCheckBox *)[self.view viewWithTag:100];

 * if ([checkBox isChecked]) {

 * // 复选框选中了

 * [checkBox setChecked:YES];

 

 * }else{

 * // 复选框未选中

 * [checkBox setChecked:NO];

 * }

 

 * }


 *

 */


1  WSYCheckBox.h 文件

#import <UIKit/UIKit.h>


@interface WSYCheckBox :UIControl

{

    // 选中状态的改变

   BOOL isSelected;

}

// 右边名称的label

@property (nonatomic,retain, readonly)UILabel *nameLabel;


// 图标

@property (nonatomic,retain, readonly)UIImageView *imageView;


// 背景视图

@property (nonatomic,retain, readonly)UIView *bgView;


// 图标状态

@property (nonatomic,assign) BOOL checked;


// 设置标题

- (void)setCheckBoxText:(NSString *)text;


// 判断复选框是否被选中

- (BOOL)isChecked;

@end


2  WSYCheckBox.m 文件

#import "WSYCheckBox.h"


@implementation WSYCheckBox


// 设置标题的字体大小

#define kFont_size 14


@synthesize imageView, nameLabel,bgView;


- (id)initWithFrame:(CGRect)frame

{

   self = [superinitWithFrame:frame];

   if (self) {

        

       CGFloat wedth = frame.size.width;

       CGFloat height = frame.size.height;

        

       CGFloat labelW = wedth-height;

       CGFloat kEdge = height/5;

        

        // 创建背景视图

       bgView = [[UIViewalloc] initWithFrame:CGRectMake(0,0,wedth,height )];

        bgView.backgroundColor =self.backgroundColor;

        [selfaddSubview:bgView];

        

       // 创建图标

       imageView = [[UIImageViewalloc] initWithFrame:CGRectMake(0, kEdge, wedth-labelW-kEdge*2,height-kEdge*2)];

        [bgView addSubview:imageView];

        

        // 创建名称标签

       nameLabel = [[UILabelalloc] initWithFrame:CGRectMake(imageView.frame.size.width+kEdge,0, labelW, height)];

        nameLabel.textAlignment =NSTextAlignmentLeft;

        nameLabel.textColor = [UIColorcolorWithRed:0green:0blue:0alpha:0.7];

       nameLabel.font = [UIFontsystemFontOfSize:kFont_size];

        [bgView addSubview:nameLabel];

        

        //设置复选框的默认状态为未选中

        [selfsetChecked:NO];

        

    }

    return self;

}


/**

 * 设置状态

 */

- (void)setChecked:(BOOL)checked

{

    isSelected = !isSelected;

   if (!isSelected) {

       imageView.image = [UIImageimageNamed:@"checked.png"];

    }else{

       imageView.image = [UIImageimageNamed:@"unchecked.png"];

    }

}


/**

 * 设置复选框的名称

 */

- (void)setCheckBoxText:(NSString *)text

{

   nameLabel.text = text;

}


/**

 * 复选框的状态

 */

- (BOOL)isChecked

{

    returnisSelected;

}



0 0
原创粉丝点击