UIButton的使用
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; //UIButton的创建 /* button的类型 UIButtonTypeCustom 自定义类型(常用类型) UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),(系统自带类型) 显示详情常用的样式 UIButtonTypeDetailDisclosure UIButtonTypeInfoLight UIButtonTypeInfoDark 显示为加号 UIButtonTypeContactAdd */ UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; //设置button的位置 button.frame = CGRectMake(100, 100, 200, 50); //设置正常状态下的图片 setImage图片不会被拉伸 //如果是setgroundImage图片会被拉伸; [button setImage:[UIImage imageNamed:@"back_on.png"] forState:UIControlStateNormal]; //给button设置标题 [button setTitle:@"确定" forState:UIControlStateNormal]; //设置button文本的颜色 [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; button.backgroundColor = [UIColor redColor]; //设置文本的背景颜色 button.titleLabel.backgroundColor = [UIColor greenColor]; //设置文本的字体 button.titleLabel.font = [UIFont systemFontOfSize:20]; //文本加粗 button.titleLabel.font = [UIFont boldSystemFontOfSize:20]; //设置button的点击事件 /* addTarget:<#(id)#> action:<#(SEL)#> forControlEvents:<#(UIControlEvents)#> */ [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button];}- (void)buttonAction{ NSLog(@"按钮被点击了");}
UILable的使用
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. /*********UILable**********/ //显示文本,少量文本,大量文本 - >textView //1.创建 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 50)]; //2.文本 label.text = @"设置label的文本设置label的文本设置label的文本设置label的文本设置label的文本设置label的文本"; //3.背景颜色,默认为白色 label.backgroundColor = [UIColor orangeColor]; //4.对齐方式,默认居左对齐 label.textAlignment = NSTextAlignmentCenter; //5.设置字体颜色 label.textColor = [UIColor blueColor]; //6.设置字体样式和字体大小 //获取系统字体名称 NSArray *fontArray = [UIFont familyNames]; NSLog(@"%@",fontArray); //设置字体样式和大小 //UIFont *font = [UIFont fontWithName:@"Zapfino" size:20]; //label.font = font; //7.行数,默认是1行,0表示自动换行 label.numberOfLines = 0; //8.文本的自适应大小 /* 如果是一行显示,sizeToFit会自动向后调整宽度,一直到视图边缘 如果是自动换行,sizeToFit会自动调整高度 */ [label sizeToFit]; //9.设置文本的阴影 label.shadowColor = [UIColor lightGrayColor]; //设置阴影的颜色 label.shadowOffset = CGSizeMake(1, 1); //设置阴影的偏移量 [self.view addSubview:label]; }
UIImageView的使用
/********UIImageView 图片视图*********/ //1.创建 UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 200, 200)]; imgView.backgroundColor = [UIColor cyanColor]; //2.设置图片 imgView.image = [UIImage imageNamed:@"checkbox_empty"]; //3.设置高亮图片 imgView.highlightedImage = [UIImage imageNamed:@"checkbox_full"]; //Highlighted:默认是NO,表示非高亮状态 //imgView.Highlighted = YES; UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame = CGRectMake(50, 50, 100, 100); btn.backgroundColor = [UIColor orangeColor]; [imgView addSubview:btn]; //图片视图默认不接受时间响应,如果添加button,需要开启userInteractionEnabled imgView.userInteractionEnabled = YES; [btn setImage:[UIImage imageNamed:@"checkbox_empty"] forState:UIControlStateHighlighted]; //初始化同时传入图片 UIImageView *imgView1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"checkbox_full"]]; imgView1.frame = CGRectMake(100, 300, 50, 50); [self.view addSubview:imgView1]; //加载网络图片 //1.url NSURL *url = [NSURL URLWithString:@"http://img0.bdstatic.com/img/image/shouye/mingxing0415.jpg"]; NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *img = [UIImage imageWithData:data]; UIImageView *imgView2 = [[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 300, 300)]; //imgView2.image = img; [self.view addSubview:imgView2]; [self.view addSubview:imgView]; UIImageView *imgView3 = [[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 300, 300)]; //arr.count NSMutableArray *arr = [NSMutableArray array]; // -> alloc -->init for (int i = 1; i <= 20 ; i ++) { NSString *name = [NSString stringWithFormat:@"%d.jpg",i]; UIImage *img = [UIImage imageNamed:name]; [arr addObject:img]; } //逐帧动画 //设置动画数组 imgView3.animationImages = arr; //设置动画时长 imgView3.animationDuration = 2.5; //开始动画 [imgView3 startAnimating]; //[imgView3 stopAnimating]; [self.view addSubview:imgView3]; }
UITextField的使用
//1.创建 UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 200, 50)]; //textField.backgroundColor = [UIColor orangeColor]; //2.设置边框样式 /* UITextBorderStyleNone 没有边框 UITextBorderStyleLine 线框 UITextBorderStyleBezel 线框带阴影 UITextBorderStyleRoundedRect 圆角边框 */ textField.borderStyle = UITextBorderStyleRoundedRect; //3.设置或者获取文本内容 //textField.text = @"Hello"; //NSString *str = textField.text; //4.设置提示文本 textField.placeholder = @"请输入密码"; //5.设置清除按钮 textField.clearButtonMode = UITextFieldViewModeWhileEditing; //6.文本,字体 textField.font = [UIFont italicSystemFontOfSize:20]; //7.颜色 textField.textColor = [UIColor redColor]; //8.设置首字母大写 (默认首字母大写UITextAutocapitalizationTypeWord) textField.autocapitalizationType = UITextAutocapitalizationTypeNone; //9.拼写提示(默认开启提示检查) textField.autocorrectionType = UITextAutocorrectionTypeNo; //10.设置键盘样式 /* UIKeyboardTypeNumberPad:显示数字键盘 UIKeyboardTypeDefault :默认键盘 UIKeyboardTypeURL:URL键盘,包含.com UIKeyboardTypeEmailAddress:邮箱键盘,包含@ . */ textField.keyboardType = UIKeyboardTypeEmailAddress; //11.return键样式 默认是灰色return键,其他都为蓝色键 textField.returnKeyType = UIReturnKeyDone; //12.密文输入 //textField.secureTextEntry = YES; //13.再次编辑时,清空之前的文本内容 textField.clearsOnBeginEditing = YES; //响应者(弹出键盘) //[textField becomeFirstResponder]; //收起键盘 //[textField resignFirstResponder]; //事件传递 button addtarget //设置代理 ,用来监听textField事件的发生 //self self.view textField textField.delegate = self; //让当前视图控制器作为代理 [self.view addSubview:textField]; }//将要开始编辑 (键盘将要弹出)- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ NSLog(@"将要开始编辑"); return YES;}//已经开始编辑 (键盘已经弹出)- (void)textFieldDidBeginEditing:(UITextField *)textField{ NSLog(@"已经开始编辑");}//将要结束编辑 (将要收起键盘)- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{ NSLog(@"将要结束"); return YES;}//已经结束编辑 (已经收起键盘)- (void)textFieldDidEndEditing:(UITextField *)textField{ NSLog(@"已经结束编辑");}//当return键被点击时实现的协议方法- (BOOL)textFieldShouldReturn:(UITextField *)textField{ NSLog(@"return 被点击"); //收起键盘 [textField resignFirstResponder]; return YES;}- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ NSLog(@"%@",string); //过滤某一些敏感词 if ([string isEqualToString:@"s"]) { return NO; } return YES;}
UISlider的使用
/*******UISlider 滑动视图********/ //1.创建 UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(50, 400, 280, 50)]; //添加图片 UIImage *img = [UIImage imageNamed:@"scene1.jpg"]; UIImageView *imgView = [[UIImageView alloc]initWithImage:img]; imgView.frame = CGRectMake(50, 50, 280, 300); imgView.tag = 100; [self.view addSubview:imgView]; // slider.backgroundColor = [UIColor orangeColor]; //2.value 表示滑动的范围,float值 slider.minimumValue = 0.0; slider.maximumValue = 1.0; slider.value = 1; //修改两次滑动条的颜色(不太常用,使用的话也要颜色) slider.minimumTrackTintColor = [UIColor redColor]; slider.maximumTrackTintColor = [UIColor greenColor]; //设置拇指图片,绑定状态,在正常状态下 [slider setThumbImage:[UIImage imageNamed:@"checkbox_full"] forState:UIControlStateNormal]; [slider setThumbImage:[UIImage imageNamed:@"checkbox_empty"] forState:UIControlStateHighlighted]; //绑定事件,让视图渐隐或出现 [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:slider]; }//参数类型是UISlider- (void)sliderAction:(UISlider *)slider{ NSLog(@"%f",slider.value); //将value的值作为透明度 UIImageView *imgView = (UIImageView *)[self.view viewWithTag:100]; imgView.alpha = slider.value; }