iOS的應(yīng)用程序是運行在沙盒中的,也就是說各個應(yīng)用無法訪問除自己外的其它應(yīng)用的地址抗愁。
接下來,我將對iOS開發(fā)中常用的組件NSButton茬高,NSImage堵第,NSLabel做一個總結(jié)吧凉。
Xcode提供了Interface Builder來方便快速地創(chuàng)建應(yīng)用界面。
我們可以將想要的控件從這里拖到view上型诚,完成添加客燕。
通過Outlet插座,按住Ctrl+控件拖拽到對應(yīng)的ViewController的.h文件中可以將IB中的組件添加到代碼里狰贯,方便代碼的操作也搓。包括添加屬性(IBOutlet)和相應(yīng)方法(IBAction)。
通過代碼添加控件
把控件加入頁面view[self.view addSubview:(UIView)]
1 這個方法會retain一次view涵紊,并且設(shè)置它的下一個響應(yīng)者是receiver傍妒,即它的新的父視圖。
2 每一個視圖只能有唯一的一個父視圖摸柄。如果當前操作視圖已經(jīng)有另外的一個父視圖颤练,則addsubview的操作會把它先從上一個父視圖中移除(包括響應(yīng)者鏈),再加到新的父視圖上面驱负。
UILabel
1 建立UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
2
3 常用屬性 //設(shè)置顯示文字
4 label.text = @"label1";
5 //設(shè)置字體:粗體嗦玖,正常的是 SystemFontOfSize
6 label.font = [UIFont boldSystemFontOfSize:20];
7 //設(shè)置文字顏色
8 label.textColor = [UIColor orangeColor];
9 //設(shè)置文字排版方式
10 label.textAlignment = UITextAlignmentRight;
11 label.textAlignment = UITextAlignmentCenter;
12 //設(shè)置字體大小適應(yīng)label寬度
13 label.adjustsFontSizeToFitWidth = YES;
14 //設(shè)置label的顯示行數(shù)
15 label.numberOfLines = 2;
16 //背景色,也可以設(shè)置背景圖
17 label.backgroundColor=[UIColor clearColor]; //可以去掉背景色
18 //設(shè)置高亮
19 label.highlighted = YES;
20 label.highlightedTextColor = [UIColor orangeColor];
21 //設(shè)置陰影
22 label.shadowColor = [UIColor redColor];
23 label.shadowOffset = CGSizeMake(1.0,1.0);
24 //設(shè)置是否能與用戶進行交互
25 label.userInteractionEnabled = YES;
26 //設(shè)置label中的文字是否可變跃脊,默認值是YES
27 label.enabled = NO;
28 //設(shè)置文字過長時的顯示格式
29 label.lineBreakMode = UILineBreakModeMiddleTruncation;//截去中間
30 // typedef enum {
31 // UILineBreakModeWordWrap = 0,
32 // UILineBreakModeCharacterWrap,
33 // UILineBreakModeClip,//截去多余部分
34 // UILineBreakModeHeadTruncation,//截去頭部
35 // UILineBreakModeTailTruncation,//截去尾部
36 // UILineBreakModeMiddleTruncation,//截去中間
37 // } UILineBreakMode;
38
UIButton
1 建立 //UIButton的定義
2 UIButton *button=[[UIButton buttonWithType:(UIButtonType);
3 //typedef enum {
4 // UIButtonTypeCustom = 0, 自定義風(fēng)格
5 // UIButtonTypeRoundedRect, 圓角矩形
6 // UIButtonTypeDetailDisclosure, 藍色小箭頭按鈕宇挫,主要做詳細說明用
7 // UIButtonTypeInfoLight, 亮色感嘆號
8 // UIButtonTypeInfoDark, 暗色感嘆號
9 // UIButtonTypeContactAdd, 十字加號按鈕
10 //} UIButtonType;
11
12 常用方法//設(shè)置frame
13 button.frame = CGRectMake(20, 20, 280, 40);
14 [button setFrame:CGRectMake(20,20,50,50)];
15
16 //button背景色
17 button.backgroundColor = [UIColor clearColor];
18 [button setBackgroundColor:[UIColor blueColor]];
19
20 //是否可點擊,默認為YES
21 button.enable = NO;
22 //設(shè)置button填充圖片和背景圖片
23 [button setImage:(UIImage) forState:UIControlStateNormal];
24 [button setBackgroundImage:(UIImage)forState:UIControlStateNormal];
25 //設(shè)置button標題和標題顏色
26 [button1 setTitle:@"點擊" forState:UIControlStateNormal];
27 [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
28
29 //添加或刪除事件處理
30 [button addTarget:self action:@selector(butClick:)forControlEvents:UIControlEventTouchUpInside];
31 [button removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
32
UIImageView
1 建立UIImageView *imageView = [[UIImageView alloc ] init];
2 //UIImage加載有兩種方式
3 //方案1
4 UIImage *image = [UIImage imageNamed:@"image_photo"];
5 //方案2
6 NSString *filePath=[[NSBundle mainBundle] pathForResource:@"image_photo" ofType:@"jpg"];
7 UIImage *image=[UIImage imageWithContentsOfFile:filePath];
8
9 imageView.image = image;
10
方案一:用imageNamed的方式加載時酪术,系統(tǒng)會把圖像Cache到內(nèi)存器瘪。如果圖像比較大,或者圖像比較多绘雁,用這種方式會消耗很大的內(nèi)存橡疼,而且釋放圖像的內(nèi)存是一件相對來說比較麻煩的事情。例如:如果利用imageNamed的方式加載圖像到一個動態(tài)數(shù)組NSMutableArray庐舟,然后將將數(shù)組賦予一個UIView的對象的animationImages進行逐幀動畫欣除,那么這將會很有可能造成內(nèi)存泄露。并且釋放圖像所占據(jù)的內(nèi)存也不會那么簡單继阻。但是利用imageNamed加載圖像也有自己的優(yōu)勢耻涛。對于同一個圖像系統(tǒng)只會把它Cache到內(nèi)存一次,這對于圖像的重復(fù)利用是非常有優(yōu)勢的瘟檩。例如:你需要在一個TableView里重復(fù)加載同樣一個圖標抹缕,那么用imageNamed加載圖像,系統(tǒng)會把那個圖標Cache到內(nèi)存墨辛,在Table里每次利用那個圖像的時候卓研,只會把圖片指針指向同一塊內(nèi)存。這種情況使用imageNamed加載圖像就會變得非常有效。方案二:就是每次單獨加載圖片奏赘,系統(tǒng)不緩存寥闪。使用完后記得釋放。
1 常用方法 //設(shè)置圓角
2 imageView.layer.masksToBounds = YES;
3 imageView.layer.cornerRadius = 10;
4
5 //設(shè)置邊框顏色和大小
6 imageView.layer.borderColor = [UIColor orangeColor].CGColor;
7 imageView.layer.borderWidth = 2;
8
9 //播放一系列圖片
10 UIImage *image1 = [UIImage imageNamed:@"1"];
11 UIImage *image2 = [UIImage imageNamed:@"2"];
12 UIImage *image3 = [UIImage imageNamed:@"3"];
13 NSArray *imagesArray = @[image1,image2,image3];
14 imageView.animationImages = imagesArray;
15 // 設(shè)定所有的圖片在多少秒內(nèi)播放完畢
16 imageView.animationDuration = [imagesArray count];
17 // 重復(fù)播放多少遍磨淌,0表示無數(shù)遍
18 imageView.animationRepeatCount = 0;
19 // 開始播放
20 [imageView startAnimating];
21
22 //為圖片添加單擊事件:一定要先將userInteractionEnabled置為YES疲憋,這樣才能響應(yīng)單擊事件
23 imageView.userInteractionEnabled = YES;
24 UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
25 [imageView addGestureRecognizer:singleTap];
26
27 //其他設(shè)置
28 imageView.hidden = YES或者NO; // 隱藏或者顯示圖片
29 imageView.alpha =0.5; // 設(shè)置透明度
30 // 設(shè)置高亮?xí)r顯示的圖片
31 imageView.highlightedImage = (UIImage *)hightlightedImage;
32 imageView.image = (UIImage *)image; // 設(shè)置正常顯示的圖片
UITextField
1 建立UITextField* textField = [[UITextField alloc] initWithFrame:CGRectMake(x, y, width, height)];
2
3 常用方法textField setBorderStyle:UITextBorderStyleRoundedRect]; //外框類型
4 textField.placeholder = @"password"; //默認顯示的字
5 textField.secureTextEntry = YES; //密碼
6
7 textField.autocorrectionType = UITextAutocorrectionType; //設(shè)置是否自動糾錯
8 typedef enum {
9 UITextAutocorrectionTypeDefault, //默認
10 UITextAutocorrectionTypeNo, //不自動糾錯
11 UITextAutocorrectionTypeYes, //自動糾錯
12 }UITextAutocorrectionType;
13
14 textField.clearButtonMode = UITextFieldViewMode; //編輯時會出現(xiàn)個修改X
15 typedef enum {
16 UITextFieldViewModeNever, //重不出現(xiàn)
17 UITextFieldViewModeWhileEditing, //編輯時出現(xiàn)
18 UITextFieldViewModeUnlessEditing, //除了編輯外都出現(xiàn)
19 UITextFieldViewModeAlways //一直出現(xiàn)
20 }UITextFieldViewMode;
21
22 //可以在UITextField使用下面方法,按return鍵返回
23 -(IBAction) textFieldDone:(id) sender
24 {
25 [textFieldName resignFirstResponder];
26 }
27 //鏈接TextField控件的"Did end on exit"
28
29 //再次編輯就清空
30 textField.clearsOnBeginEditing = YES;
31
32 //設(shè)置鍵盤樣式
33 textField.keyboardType = UIKeyboardType;
34 typedef enum {
35 UIKeyboardTypeDefault, //默認鍵盤梁只,支持所有字符
36 UIKeyboardTypeASCIICapable, //支持ASCII的默認鍵盤
37 UIKeyboardTypeNumbersAndPunctuation, //標準電話鍵盤缚柳,支持+*#字符
38 UIKeyboardTypeURL, //URL鍵盤,支持.com按鈕 只支持URL字符
39 UIKeyboardTypeNumberPad, //數(shù)字鍵盤
40 UIKeyboardTypePhonePad, //電話鍵盤
41 UIKeyboardTypeNamePhonePad, //電話鍵盤搪锣,也支持輸入人名
42 UIKeyboardTypeEmailAddress, //用于輸入電子郵件地址的鍵盤
43 UIKeyboardTypeDecimalPad, //數(shù)字鍵盤有數(shù)字和小數(shù)點
44 UIKeyboardTypeTwitter, //優(yōu)化的鍵盤秋忙,方便輸入@、#字符
45 UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
46 }UIKeyboardType;
47
48 //return鍵樣式
49 text.returnKeyType =UIReturnKeyType;
50 typedef enum {
51 UIReturnKeyDefault, 默認 灰色按鈕构舟,標有Return
52 UIReturnKeyGo, 標有Go的藍色按鈕
53 UIReturnKeyGoogle,標有Google的藍色按鈕灰追,用語搜索
54 UIReturnKeyJoin,標有Join的藍色按鈕
55 UIReturnKeyNext,標有Next的藍色按鈕
56 UIReturnKeyRoute,標有Route的藍色按鈕
57 UIReturnKeySearch,標有Search的藍色按鈕
58 UIReturnKeySend,標有Send的藍色按鈕
59 UIReturnKeyYahoo,標有Yahoo的藍色按鈕
60 UIReturnKeyYahoo,標有Yahoo的藍色按鈕
61 UIReturnKeyEmergencyCall, 緊急呼叫按鈕
62 } UIReturnKeyType;