分享

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

 爽行天下丶 2015-06-19

代码如下:

/**

 * 使用方法:

 * // 创建复选框

 * - (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 = [super initWithFrame:frame];

    if (self) {

        

        CGFloat wedth = frame.size.width;

        CGFloat height = frame.size.height;

        

        CGFloat labelW = wedth-height;

        CGFloat kEdge = height/5;

        

        // 创建背景视图

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

        bgView.backgroundColor = self.backgroundColor;

        [self addSubview:bgView];

        

        // 创建图标

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

        [bgView addSubview:imageView];

        

        // 创建名称标签

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

        nameLabel.textAlignment = NSTextAlignmentLeft;

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

        nameLabel.font = [UIFont systemFontOfSize:kFont_size];

        [bgView addSubview:nameLabel];

        

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

        [self setChecked:NO];

        

    }

    return self;

}


/**

 * 设置状态

 */

- (void)setChecked:(BOOL)checked

{

    isSelected = !isSelected;

    if (!isSelected) {

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

    }else{

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

    }

}


/**

 * 设置复选框的名称

 */

- (void)setCheckBoxText:(NSString *)text

{

    nameLabel.text = text;

}


/**

 * 复选框的状态

 */

- (BOOL)isChecked

{

    return isSelected;

}



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多