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