#define Tag 200
#define CRM(x, y, width, height) CGRectMake(x, y, width, height)
@interface ViewController ()
@property(nonatomic,strong)UILabel *label;//标签
@property(nonatomic,strong)UITextField *textField;//文本框
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_label = [[UILabel alloc]initWithFrame:CRM(150, 200, 100, 50)];//初始化位置大小
_label.font = [UIFont systemFontOfSize:20];//字体大小
_label.backgroundColor = [UIColor redColor];//文本颜色
_label.text =@"dsad";//文本内容
_label.textAlignment = NSTextAlignmentCenter;//对其中心
_label.numberOfLines = 0;//显示行数
[self.view addSubview:_label];//加载父视图
_textField = [[UITextField alloc]initWithFrame:CRM(150, 300, 100, 50)];
_textField.placeholder = @"请输入密码";//占位符
_textField.borderStyle = UITextBorderStyleRoundedRect;//边框
_textField.secureTextEntry = YES;
[self.view addSubview:_textField];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];//系统自带方法 system
button.frame = CRM(150, 350, 100, 50); //位置大小
button.tag = Tag; //为tag赋值
button.backgroundColor = [UIColor redColor];//背景颜色
[button setTitle:@"确定" forState:UIControlStateNormal];//添加标题
[button addTarget:self action:@selector(buttonT:) forControlEvents:UIControlEventTouchUpInside];//点击事件
[self.view addSubview:button];
UIButton *exchangeColorButton = [UIButton buttonWithType:UIButtonTypeSystem];
exchangeColorButton.bounds = CRM(0, 0, 100, 50);//大小
exchangeColorButton.center = CPM(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame)+200);//中心点的位置
exchangeColorButton.backgroundColor = [UIColor orangeColor];
[exchangeColorButton setTitle:@"变色" forState:UIControlStateNormal];
[exchangeColorButton addTarget:self action:@selector(exchangeColorButtonT:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:exchangeColorButton];
}
-(void)buttonT:(UIButton *)sender{
if (_textField.text.length > 0) {
_label.text = _textField.text;//把文本赋值给标签
}else{
NSLog(@"至少输入一个字符");
}
[_textField resignFirstResponder];//回收键盘
}
-(void)exchangeColorButtonT:(UIButton *)sender{
_label.backgroundColor = [UIColor orangeColor];
UIButton *currentButton = [self.view viewWithTag:Tag];
currentButton.backgroundColor = [UIColor orangeColor];
}
#define CRM(x, y, width, height) CGRectMake(x, y, width, height)
@interface ViewController ()
@property(nonatomic,strong)UILabel *label;//标签
@property(nonatomic,strong)UITextField *textField;//文本框
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_label = [[UILabel alloc]initWithFrame:CRM(150, 200, 100, 50)];//初始化位置大小
_label.font = [UIFont systemFontOfSize:20];//字体大小
_label.backgroundColor = [UIColor redColor];//文本颜色
_label.text =@"dsad";//文本内容
_label.textAlignment = NSTextAlignmentCenter;//对其中心
_label.numberOfLines = 0;//显示行数
[self.view addSubview:_label];//加载父视图
_textField = [[UITextField alloc]initWithFrame:CRM(150, 300, 100, 50)];
_textField.placeholder = @"请输入密码";//占位符
_textField.borderStyle = UITextBorderStyleRoundedRect;//边框
_textField.secureTextEntry = YES;
[self.view addSubview:_textField];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];//系统自带方法 system
button.frame = CRM(150, 350, 100, 50); //位置大小
button.tag = Tag; //为tag赋值
button.backgroundColor = [UIColor redColor];//背景颜色
[button setTitle:@"确定" forState:UIControlStateNormal];//添加标题
[button addTarget:self action:@selector(buttonT:) forControlEvents:UIControlEventTouchUpInside];//点击事件
[self.view addSubview:button];
UIButton *exchangeColorButton = [UIButton buttonWithType:UIButtonTypeSystem];
exchangeColorButton.bounds = CRM(0, 0, 100, 50);//大小
exchangeColorButton.center = CPM(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame)+200);//中心点的位置
exchangeColorButton.backgroundColor = [UIColor orangeColor];
[exchangeColorButton setTitle:@"变色" forState:UIControlStateNormal];
[exchangeColorButton addTarget:self action:@selector(exchangeColorButtonT:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:exchangeColorButton];
}
-(void)buttonT:(UIButton *)sender{
if (_textField.text.length > 0) {
_label.text = _textField.text;//把文本赋值给标签
}else{
NSLog(@"至少输入一个字符");
}
[_textField resignFirstResponder];//回收键盘
}
-(void)exchangeColorButtonT:(UIButton *)sender{
_label.backgroundColor = [UIColor orangeColor];
UIButton *currentButton = [self.view viewWithTag:Tag];
currentButton.backgroundColor = [UIColor orangeColor];
}