UI UILabel Button UITextField

#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];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值