標籤:
動態建立控制項一、按鈕二、文本輸入框三、lable標籤
注意:
只是簡單的拖拽控制項會毀了你,所以最好還是手動通過代碼建立控制項。
如果要通過代碼產生按鈕的話,可以在系統內建的函數viewDidLoad實現。應為每個控制項都對應一個類,所以可以直接通過類建立一個對象,也就是一個控制項,然後再逐步設定控制項的屬性。
下面這些操作基本上都是通用的,在不同的控制項下操作基本相同一、按鈕1、建立對象,這沒啥好說的
UIButton *btn = [[UIButton alloc] init];
2、在顯示到視圖控制器之前還要設定一些基本屬性
1.1 設定按鈕普通狀態下得屬性
// 設定背景圖片
UIImage *image = [UIImage imageNamed:@"btn_01.png"];
[btn setBackgroundImage:image forState:UIControlStateNormal];
// 設定顯示的文字
[btn setTitle:@"come on" forState:UIControlStateNormal];
// 設定文字顏色
UIColor *color = [UIColor colorWithRed:0 green:0 blue:124 alpha:1];
[btn setTitleColor:color forState:UIControlStateNormal];
UIControlStateNormal、UIControlStateHighlighted表示按鈕的狀態,表示普通模式和高亮模式
1.2 設定高亮狀態下得屬性
image = [UIImage imageNamed:@"btn_02.png"];
[btn setBackgroundImage:image forState:UIControlStateHighlighted];
// 設定顯示的文字
[btn setTitle:@"去死" forState:UIControlStateHighlighted];
// 設定文字顏色
color = [UIColor colorWithRed:200 green:0 blue:0 alpha:1];
[btn setTitleColor:color forState:UIControlStateHighlighted];
3、建立後要把對象添加到視圖,這樣才能顯示出來。
[self.view addSubview:btn]; // 添加按鈕到控制器中得viwe中
具體過程就看代碼吧。
1 #import "SLQViewController.h" 2 3 const int DELTA = 5; 4 @interface SLQViewController () 5 6 @end 7 8 @implementation SLQViewController 9 10 // 控制器的view載入完畢會調用一次11 - (void)viewDidLoad12 {13 [super viewDidLoad];14 // Do any additional setup after loading the view, typically from a nib.15 NSLog(@"viewdidload-----");16 17 // 1、建立按鈕18 UIButton *btn = [[UIButton alloc] init];19 20 // 設定位置和尺寸21 btn.frame = CGRectMake(0, 0, 100, 100);22 // 設定按鈕普通狀態下得屬性23 // 設定背景圖片24 UIImage *image = [UIImage imageNamed:@"btn_01.png"];25 [btn setBackgroundImage:image forState:UIControlStateNormal];26 // 設定顯示的文字27 [btn setTitle:@"come on" forState:UIControlStateNormal];28 // 設定文字顏色29 UIColor *color = [UIColor colorWithRed:0 green:0 blue:124 alpha:1];30 [btn setTitleColor:color forState:UIControlStateNormal];31 //;32 //[btn setTitleColor:<#(UIColor *)#> forState:<#(UIControlState)#>];33 34 // 設定高亮狀態下得屬性35 image = [UIImage imageNamed:@"btn_02.png"];36 [btn setBackgroundImage:image forState:UIControlStateHighlighted];37 // 設定顯示的文字38 [btn setTitle:@"去死" forState:UIControlStateHighlighted];39 // 設定文字顏色40 color = [UIColor colorWithRed:200 green:0 blue:0 alpha:1];41 [btn setTitleColor:color forState:UIControlStateHighlighted];42 43 44 45 // 監聽按鈕點擊事件46 [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];47 // 2、添加按鈕到控制器中得viwe中48 [self.view addSubview:btn];49 }50 51 - (void)didReceiveMemoryWarning52 {53 [super didReceiveMemoryWarning];54 // Dispose of any resources that can be recreated.55 }56 57 58 - (void)btnClick:(UIButton *)btn59 {60 NSLog(@"點擊按鈕:%p",btn);61 }二、文本輸入框 建立一個文本輸入框
1 // 文字框2 UITextField *text = [[UITextField alloc] init]; // 建立對象3 text.frame = CGRectMake(100, 0, 100, 100); // 設定位置和尺寸4 text.backgroundColor = [UIColor redColor]; // 設定背景顏色5 [self.view addSubview:text]; // 添加到視圖控制器的view中
效果這樣
三、lable標籤建立lable標籤
1 // lable標籤2 UILabel *lable = [[UILabel alloc] init]; // 建立lable對象3 lable.frame = CGRectMake(100, 100, 100,100); // 設定位置和尺寸4 lable.text = @"this is a lable"; // 設定顯示文本5 lable.textColor = [UIColor colorWithRed:255 green:0 blue:0 alpha:1]; // 設定RGBA值6 lable.font = [UIFont systemFontOfSize:10];7 [self.view addSubview:lable]; // 添加到view中
關於這個控制項包含的屬性和方法可以直接去源檔案裡尋找。比如說lable的下列可操作屬性。
1 @property(nonatomic,copy) NSString *text; // default is nil2 @property(nonatomic,retain) UIFont *font; // default is nil (system font 17 plain)3 @property(nonatomic,retain) UIColor *textColor; // default is nil (text draws black)4 @property(nonatomic,retain) UIColor *shadowColor; // default is nil (no shadow)5 @property(nonatomic) CGSize shadowOffset; // default is CGSizeMake(0, -1) -- a top shadow6 @property(nonatomic) NSTextAlignment textAlignment; // default is NSTextAlignmentLeft7 @property(nonatomic) NSLineBreakMode lineBreakMode; // default is NSLineBreakByTruncatingTail.
總結:
通過代碼建立控制項的一般過程就是
1、建立一個OC類對象,其實就是對應一個控制項
比如 UIButton *btn = [[UIButton alloc] init]; //
2、對控制項的一般屬性進行設定,不然無法顯示到view中
比如設定位置尺寸、標題,字型,背景等屬性,可以去對應的類中查看可以是用的屬性和方法。
3、添加到View視圖,這樣最終才會顯示到程式中。
IOS開發學習筆記019-動態建立控制項