UIButton
summary
按钮的作用 可以和用户交互, 既能显示图片,也能显示文字
pooperty
- normal(普通状态)
- 默认情况(Default)
- 对应的枚举常量:UIControlStateNormal
- highlighted(高亮状态)
- 按钮被按下去的时候(手指还未松开)
- 对应的枚举常量:UIControlStateHighlighted
- disabled(失效状态,不可用状态)
- 如果enabled属性为NO,就是处于disable状态,代表按钮不可以被点击
- 对应的枚举常量:UIControlStateDisabled
Method
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
设置按钮的文字
btn.titleLabel.font=[UIFontsystemFontOfSize:13];
- 设置按钮的⽂文字字体(需要拿到按钮内部的label来设置)
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
设置按钮的文字颜色
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
设置按钮内部的小图片
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
设置按钮的背景图片
demo1 基本使用
// 1.1 创建按钮对象
//UIButton *button = [[UIButton alloc] init];
// 注意:设置按钮的类型只能在初始化的时候设置 -> UIButtonTypeCustom
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// 1.2 设置按钮的类型
//button.buttonType = UIButtonTypeInfoDark;
// 1.3 设置frame
button.frame = CGRectMake(100, 100, 170, 60);
// 1.4 设置背景颜色
//button.backgroundColor = [UIColor redColor];
//[button setBackgroundColor:[UIColor redColor]];
// 1.5 设置文字
// 分状态的:
//button.titleLabel.text = @"普通文字";
[button setTitle:@"普通按钮" forState:UIControlStateNormal];
[button setTitle:@"高亮按钮" forState:UIControlStateHighlighted];
// 1.6 设置文字的颜色
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];
// 1.7 设置文字的阴影颜色
[button setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
button.titleLabel.shadowOffset = CGSizeMake(3, 2);
// 1.8 设置内容图片
[button setImage:[UIImage imageNamed:@"player_btn_pause_normal"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"player_btn_pause_highlight"] forState:UIControlStateHighlighted];
//button.imageView.backgroundColor = [UIColor purpleColor];
// 1.9 设置背景图片
[button setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted];
// 2.0 加到控制器的view中
[self.view addSubview:button];
// 非常重要
/**
* 监听按钮的点击
* Target: 目标 (让谁做事情)
* action: 方法 (做什么事情-->方法)
* Events: 事件
*/
//SEL sel = @selector(clickButton:);
[button addTarget:self action:@selector(demo:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)demo:(UIButton *)btn
{
NSLog(@"%@", btn);
}