iOS開發(fā)關(guān)于基本控件的初始化及使用方法

在眾多移動應(yīng)?用中,能看到各式各樣的表格數(shù)據(jù) 。
在iOS中,要實現(xiàn)表格,數(shù)據(jù),圖片添加,按鈕點擊等方法的展示,最常用的做法就是使用UI中的基本控件來完成 。

首先在AppDelegate.m里初始化window:

    #import "AppDelegate.h"

    @interface AppDelegate ()

    @end

    @implementation AppDelegate
    - (void)dealloc
    {
    [_window release];                  //釋放window
    [super dealloc];
    }
#pragma make -- app啟動完成走這個方法
  - (BOOL)application:(UIApplication *)application       didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

#//初始化Window對象
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
#//設(shè)置顏色:
self.window.backgroundColor = [UIColor whiteColor];
#//讓window可見:
[self.window makeKeyAndVisible];
#//為window創(chuàng)建根視圖控制器:
UIViewController *vc = [[UIViewController alloc] init];
#//將vc設(shè)置為window的根視圖控制器:
self.window.rootViewController = vc;

UILable : (標(biāo)簽控件的常用屬性)

  //初始化標(biāo)簽控件:
     UILabel *label = [[UILabel alloc]  
     initWithFrame:CGRectMake(100, 200, 175, 100)];

//設(shè)置背景顏色:
label.backgroundColor = [UIColor grayColor];
//設(shè)置label要顯示的文字:
label.text = @"Hello student!";
//設(shè)置文字對齊方式:(左中右三種 0.1.2)
label.textAlignment = NSTextAlignmentCenter;    //給1也行
//設(shè)置字體顏色:
label.textColor = [UIColor yellowColor];
//設(shè)置字體和大小:
//    label.font = [UIFont fontWithName:@"Zapfino" size:20];
  //設(shè)置顯示的行數(shù)(換行):(0表示自動去換行)
label.numberOfLines = 0;
//設(shè)置斷行模式:
 label.lineBreakMode = NSLineBreakByWordWrapping;   //按 
 字母用Char換Word

UITextField :(文本框的常用屬性)

 //初始化文本框:
 UITextField *textfield = [[UITextField alloc] 
initWithFrame:CGRectMake(100, 100, 175, 50)];

//設(shè)置提示文字:
textfield.placeholder = @"請輸入密碼:";

//設(shè)置邊框樣式:
textfield.borderStyle = UITextBorderStyleRoundedRect;

//設(shè)置邊框?qū)挾群皖伾?
textfield.layer.borderWidth = 5.0;
textfield.layer.borderColor = [UIColor blueColor].CGColor

//設(shè)置文本顏色:
textfield.textColor = [UIColor redColor];

//設(shè)置文本對齊方式:(0.1.2  左 中 右)
textfield.textAlignment = 0;

//設(shè)置字體:
textfield.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];    
 #注意字體的書寫一定要標(biāo)準(zhǔn)

//是否允許編輯:
textfield.enabled = YES;  //(NO 不可編輯,默認(rèn)為yes)

//開始輸入時清空之前的內(nèi)容:
textfield.clearsOnBeginEditing = NO;    //(NO就清空了,默認(rèn)為 YES)

//設(shè)置密碼輸入模式(小點):
textfield.secureTextEntry = YES;      //no為正常輸入

//設(shè)置鍵盤的樣式:
 //    textfield.keyboardType = UIKeyboardTypeDefault;     
//  比如只能輸入數(shù)字textfield.keyboardType =   UIKeyboardTypeNumberPad;

//設(shè)置鍵盤右下角按鈕內(nèi)容:
textfield.returnKeyType = UIReturnKeyNext;  //有很多種

UIButton :(按鈕的常用屬性)

 //初始化按鈕:
 UIButton *button = [UIButton   buttonWithType:UIButtonTypeCustom];
//設(shè)置位子
button.frame = CGRectMake(100, 100, 175, 64);
//設(shè)置按鈕文字:
[button setTitle:@"我是按鈕" forState:UIControlStateNormal];

 //設(shè)置邊框:
 //    button.layer.borderWidth = 2.0;

 //設(shè)置邊框顏色:
//    button.layer.borderColor= [UIColor greenColor].CGColor;

//設(shè)置背景顏色
 // button.backgroundColor = [UIColor blackColor];

//設(shè)置圓角:
//    button.layer.cornerRadius = 10;
 
  //設(shè)置背景圖:
 //    [button setBackgroundImage:[UIImage    
 imageNamed:@"1.jpg"] forState:UIControlStateNormal];


  //設(shè)置前景圖
 //    [button setImage:[UIImage imageNamed:@"666.jpg"] 
 forState:UIControlStateNormal];
//    UIImage *frontImage = [button  
 imageForState:UIControlStateNormal];
//    NSLog(@"%@", frontImage);


  //    //設(shè)置按鈕字體顏色:
  [button setTitleColor:[UIColor redColor] 
 forState:UIControlStateNormal];

//修改字體大小以及字體:
//修改字體大小以及字體:
   button.titleLabel.font = [UIFont fontWithName:@"Zapfino" 
size:20];
//獲取字體顏色:
   UIColor *wordColor = [button  
titleColorForState:UIControlStateNormal];
NSLog(@"%@", wordColor);

//為按鈕button添加點擊事件:
[button addTarget:self action:@selector(didClickedDogButton:) forControlEvents:UIControlEventTouchUpInside];

//為按鈕移除點擊事件
[button removeTarget:self action:@selector(didClickedDogButton:) forControlEvents:UIControlEventTouchUpInside];

//最后將按鈕添加到window上
 [view addSubview:button];

#注意:按鈕是系統(tǒng)自動釋放 不用再release ,否則將會過度釋放

UIImageView :(圖片顯示控件的常用屬性)

//創(chuàng)建UIImageView對象:
UIImageView *imageView = [[UIImageView   
alloc]initWithFrame:self.window.frame];
//設(shè)置顏色:
//    imageView.backgroundColor = [UIColor orangeColor];
//為imageView設(shè)置圖片:
//    imageView.image = [UIImage imageNamed:@"1.jpg"];
更多圖片的屬性和方法以后會介紹,這里簡單為大家做個植物大戰(zhàn)僵尸中每個圖片及方法的實現(xiàn),希望能幫助大家理解:

# 首先我們需要將已有的圖片文件拖到自己的工程里:比如下面

BackGround.png
 //1).設(shè)置整體背景:   
 imageView.image = [UIImage     
 imageNamed:@"BackGround"];
 #這里以向日癸為例,僵尸,瓜等同理
 (因為沒找到符合簡書規(guī)格的圖片,暫不上傳 sorry)
 //2).循環(huán)創(chuàng)建動畫素材(UIImage對象)
NSMutableArray *flowerArray = [NSMutableArray array];

for (NSInteger i = 1; i <= 18; i++) {
    //拼接每個圖片的名字:

    NSString *name = [NSString stringWithFormat:@"flower%ld.tiff", i];
    UIImage *image = [UIImage imageNamed:name];
    [flowerArray addObject:image];
}
//3).創(chuàng)建單獨的UIImageView對象
UIImageView *flowerIamgeView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 80, 73, 74)];
[imageView addSubview:flowerIamgeView];
[flowerIamgeView release];
//4).將數(shù)組賦值給imageView
flowerIamgeView.animationImages = flowerArray;//********
//5).設(shè)置播放圖片的時間間隔(兩張圖片之間播放的時間差):
flowerIamgeView.animationDuration = 1.0f;
//6).設(shè)置一下重復(fù)次數(shù):
//    flowerIamgeView.animationRepeatCount = 5;
#當(dāng)重復(fù)次數(shù)為0時就一直保持圖片動態(tài)播放
//7).開始動畫
[flowerIamgeView startAnimating];

//花動起來:
[UIView animateWithDuration:10 animations:^{
     flowerIamgeView.frame = CGRectMake(200, 400, 146, 148);
}];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容