IOS-用代码画按钮(checkbox,button)

1. Button:

//    successButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    CGRect frame = CGRectMake(30, 100, 160, 40);
    self.view.backgroundColor = [UIColor whiteColor];
   successButton = [[UIButton alloc] initWithFrame:CGRectMake(30, 100, 250, 40)];
//   [successButton setFrame:frame];
    [successButton setTitle:@"GW 搜索" forState:UIControlStateNormal];
    [successButton addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
    [successButton successStyle];
    [self.view addSubview:successButton];


2.Checkbox:

   QCheckBox *_check1 = [[QCheckBox alloc] initWithDelegate:self];

    _check1.frame = CGRectMake(20, 20, 80, 40);

    [_check1 setTitle:@"苹果" forState:UIControlStateNormal];

    [_check1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    [_check1.titleLabel setFont:[UIFont boldSystemFontOfSize:13.0f]];

    [self.view addSubview:_check1];

    [_check1 setChecked:YES];

    [_check1 release];


 Class:QCheckBox

 

QCheckBox.h:

#import <UIKit/UIKit.h>


@protocol QCheckBoxDelegate;


@interface QCheckBox : UIButton {

    id<QCheckBoxDelegate> _delegate;

    BOOL _checked;

    id _userInfo;

}


@property(nonatomic, assign)id<QCheckBoxDelegate> delegate;

@property(nonatomic, assign)BOOL checked;

@property(nonatomic, retain)id userInfo;


- (id)initWithDelegate:(id)delegate;


@end


@protocol QCheckBoxDelegate <NSObject>


@optional


- (void)didSelectedCheckBox:(QCheckBox *)checkbox checked:(BOOL)checked;


@end


QCheckBox.m:

#import "QCheckBox.h"


#define Q_CHECK_ICON_WH                    (15.0)

#define Q_ICON_TITLE_MARGIN                (5.0)


@implementation QCheckBox


@synthesize delegate = _delegate;

@synthesize checked = _checked;

@synthesize userInfo = _userInfo;


- (id)initWithDelegate:(id)delegate {

    self = [super init];

    if (self) {

        _delegate = delegate;

        

        self.exclusiveTouch = YES;

        [self setImage:[UIImage imageNamed:@"checkbox1_unchecked.png"] forState:UIControlStateNormal];

        [self setImage:[UIImage imageNamed:@"checkbox1_checked.png"] forState:UIControlStateSelected];

        [self addTarget:self action:@selector(checkboxBtnChecked) forControlEvents:UIControlEventTouchUpInside];

    }

    return self;

}


- (void)setChecked:(BOOL)checked {

    if (_checked == checked) {

        return;

    }

    

    _checked = checked;

    self.selected = checked;

    

    if (_delegate && [_delegate respondsToSelector:@selector(didSelectedCheckBox:checked:)]) {

        [_delegate didSelectedCheckBox:self checked:self.selected];

    }

}


- (void)checkboxBtnChecked {

    self.selected = !self.selected;

    _checked = self.selected;

    

    if (_delegate && [_delegate respondsToSelector:@selector(didSelectedCheckBox:checked:)]) {

        [_delegate didSelectedCheckBox:self checked:self.selected];

    }

}


- (CGRect)imageRectForContentRect:(CGRect)contentRect {

    return CGRectMake(0, (CGRectGetHeight(contentRect) - Q_CHECK_ICON_WH)/2.0, Q_CHECK_ICON_WH, Q_CHECK_ICON_WH);

}


- (CGRect)titleRectForContentRect:(CGRect)contentRect {

    return CGRectMake(Q_CHECK_ICON_WH + Q_ICON_TITLE_MARGIN, 0,

                      CGRectGetWidth(contentRect) - Q_CHECK_ICON_WH - Q_ICON_TITLE_MARGIN,

                      CGRectGetHeight(contentRect));

}


- (void)dealloc {

    [_userInfo release];

    _delegate = nil;

    [super dealloc];

}


@end




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值