UIButton的使用api
#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的使用ide
//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; }