Button的功能很黄很暴力,即能显示文字,又能显示图片,还能随时调整内部图片和文字的位置,用的地方很多。
(1)按钮常用的四种状态:
normal(普通状态)
默认情况(Default)
对应的枚举常量:UIControlStateNormal
highlighted(高亮状态)
按钮被按下去的时候(手指还未松开)
对应的枚举常量:UIControlStateHighlighted
selected (选中状态)
选没选中由我们自行设定
对应的枚举常量:UIControlStateSelected
disabled(失效状态,不可用状态)
如果enabled属性为NO,就是处于disable状态,代表按钮不可以被点击
对应的枚举常量:UIControlStateDisabled
(2)创建button:+ (id)buttonWithType:(UIButtonType)buttonType;
typedef NS_ENUM(NSInteger, UIButtonType) {
UIButtonTypeCustom = 0, // no button type
UIButtonTypeSystem, // standard system button
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
常用的是UIButtonTypeCustom和UIButtonTypeSystem,一般选用UIButtonTypeSystem,当你需要对button进行一些个性化的设置时,比如button的选中状态与未选中状态,字体,图片这些不同时,就必须要选择UIButtonTypeCustom,不然,选中状态时,按钮的上面会出现一个蓝色的竖条条。
UIButtonTypeRoundedRect = UIButtonTypeSystem, // Deprecated, use UIButtonTypeSystem instead
};
(2)按钮的常用属性
Button.titleLabel.font = [UIFont systemFontOfSize:20]; //改变button字体大小
(3)button的描边
(4)button的四角设置弧度
(5)
(6)
(7)
(8)
(9)
(10)
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 100, 200, 100);
//[button setTitle:@"呵呵..." forState:UIControlStateNormal];
//button.backgroundColor = [UIColor lightGrayColor];
//[button setTintColor:[UIColor yellowColor]];
button.titleLabel.text = @"hehe";
button.showsTouchWhenHighlighted=YES;
[button addTarget:self action:@selector(addButton:) forControlEvents:UIControlEventTouchUpInside];
//将标签加入视图
[self.view addSubview:button];
}
-(void)addButton:(UIButton*)button{
button.selected = !button.selected;
if (button.selected) {
NSLog(@"selected");
button.backgroundColor = [UIColor lightGrayColor];
[button setTitle:@"点我干嘛!" forState:UIControlStateNormal];
NSLog(@"%@",button.currentTitle);
} else {
NSLog(@"unSelected");
[button setTitle:@"呵呵..." forState:UIControlStateNormal];
NSLog(@"%@",button.currentTitle);
}
}
---恢复内容结束---