button,textfield,label
主要代码
#import "ViewController.h"
@interface ViewController ()<UITextFieldDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGSize size=[UIScreen mainScreen].bounds.size;//获取当前设备的屏幕设计分辨率
NSLog(@"%f %f",size.width,size.height);
// Do any additional setup after loading the view, typically from a nib.
textfieldfirst=[[UITextField alloc]initWithFrame:CGRectMake(0*size.width/5+5, 200, size.width/5, 40)];//初始化控件
// textfieldfirst.backgroundColor=[UIColor redColor];
textfieldsecond=[[UITextField alloc]initWithFrame:CGRectMake(2*size.width/5+5,200, size.width/5, 40)];
// textfieldsecond.backgroundColor=[UIColor redColor];
textfieldfirst.clearButtonMode=UITextFieldViewModeAlways;// 添加清除按钮
textfieldsecond.clearButtonMode=UITextFieldViewModeAlways;
textfieldfirst.keyboardType=UIKeyboardTypeNumbersAndPunctuation;
textfieldsecond.keyboardType=UIKeyboardTypeNumbersAndPunctuation;//设置键盘类型
[textfieldfirst setBorderStyle:UITextBorderStyleLine];
[textfieldsecond setBorderStyle:UITextBorderStyleLine];//添加线框
[textfieldfirst setFont:[UIFont systemFontOfSize:24]];//设置字体大小
textfieldfirst.delegate=self;
textfieldsecond.delegate=self;
[self.view addSubview:textfieldfirst];//添加到视图
[self.view addSubview:textfieldsecond];
label=[[UILabel alloc]initWithFrame:CGRectMake(1*size.width/5+5, 200, size.width/5, 30)];
label.text=@"+";
[label setTextAlignment:NSTextAlignmentCenter];//设置居中
[self.view addSubview:label];
UIButton *btn1=[[UIButton alloc]initWithFrame:CGRectMake(3*size.width/5+5,200, size.width/5, 40)];
// btn1.backgroundColor=[UIColor redColor];
[btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];// 设置字体颜色
[btn1 setTitle:@"=" forState:UIControlStateNormal];
[btn1 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
//添加事件
[btn1 addTarget:self action:@selector(resultoutput:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:btn1];
resultlabel=[[UILabel alloc]initWithFrame:CGRectMake(4*size.width/5+5, 200, size.width/5, 40)];
[self.view addSubview:resultlabel];
}
-(void)resultoutput:(id)sender{
resultlabel.text=@"0";
resultlabel.text=[NSString stringWithFormat:@"%d",[textfieldfirst.text intValue]+[textfieldsecond.text intValue]];
}
//键盘退出
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}