iOS-一些基本控件

一、UILabel

1.UILabel(標(biāo)簽):

UILable是顯示文本的控件,UIView子類,作為子類一般是為了擴(kuò)充父類的功能UILabel擴(kuò)展了文字顯示的功能,UILabel是能顯示文字的視圖。

2.如何使用UILabel

創(chuàng)建UILabel與創(chuàng)建UIView的步驟很相似。1、開辟空間并初始化(如果本類有初始化方法,則使用自己的初始化方法;否則使用父類的)。2、設(shè)置文本控制相關(guān)的屬性3、添加到父視圖上,用以顯示4、釋放所有權(quán)(只是做了引用計(jì)數(shù)-1)

UILabel *userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 100, 100, 30)];
//要顯示的文本內(nèi)容
userNameLabel.text = @“用戶名”;
//文本內(nèi)容的顏色
userNameLabel.textColor = [UIColor redColor];
//設(shè)置字體樣式和大小 ,黑體加粗,20號(hào)字。
userNameLabel.font = [UIFont fontWithName:@“Helvetica-Bold” size:20];
//行數(shù) 
userNameLabel.numberOfLines = 3;//顯示3行,注意label的高度要能容納3行。如果3行沒能顯示完信息,沒顯示的信息以省略號(hào)代替。
//斷行模式
userNameLabel.lineBreakMode = NSLineBreakByWordWrapping;//以單詞為單位換行
//陰影顏色
userNameLabel.shadowColor = [UIColor yellowColor];
//陰影大小
userNameLabel.shadowOffset = CGSizeMake(2,1);//陰影向x正方向偏移2,向y正方向偏移1。
//將userNameLabel添加添加到父視圖上
[self.view addSubview:userNameLabel];
[userNameLabel release];

二、UITextField(輸入框)

UITextField(輸入框):是控制文本輸入和顯示的控件。在App中UITextField出現(xiàn)頻率也比較高。
iOS系統(tǒng)借助虛擬鍵盤實(shí)現(xiàn)輸入,當(dāng)點(diǎn)擊輸入框,系統(tǒng)會(huì)自動(dòng)調(diào)出鍵盤,方便你進(jìn)一步操作。在你不需要輸入的時(shí)候,可以使用收回鍵盤的方法,收回彈出的鍵盤。
UITextField和UILabel相比,UILabel主要用于文字顯示,不能編輯,UITextField允許用戶編輯文字(輸入)

1.如何使用UITextField

創(chuàng)建UITextField與創(chuàng)建UILabel的步驟很相似。1、開辟空間并初始化(如果本類有初始化方法,則使用自己的初始化方法;否則使用父類的)。2、設(shè)置文本顯示、輸入等相關(guān)的屬性3、添加到父視圖上,用以顯示4、釋放對(duì)象所有權(quán).

// 使用初始化方法創(chuàng)建對(duì)象
   UITextField *userNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 190, 30)];

// 設(shè)置邊框風(fēng)格
   userNameTextField.borderStyle = UITextBorderStyleRoundedRect;

// 設(shè)置占位符
   textField.placeholder = @“手機(jī)號(hào)/郵箱";
//1.UITextField 文本顯示屬性
//要顯示的文本內(nèi)容
textField.text = @“你好”;
//文本內(nèi)容的顏色
textField.textColor = [UIColor redColor];
//文本的對(duì)齊方式(水平方向)
textField.textAlignment = NSTextAlignmentLeft;
//文本字體
textField.font = [UIFont fontWithName:@“Helvetica-Bold” size:20];//黑體加粗,20號(hào)字。
//占位字符串(沒有任何輸入時(shí),給出的提示字符串)
textField.placeholder = @“請(qǐng)輸入用戶名”;
//2.UITextField 輸入控制屬性
//是否允許輸入
textField.enabled =NO;//不允許輸入,不彈出鍵盤textField.enabled =YES;//默認(rèn)是YES。允許輸入
//是否開始輸入的時(shí)候清空輸入框內(nèi)容
textField.clearsOnBeginEditing = YES;//清空textField.clearsOnBeginEditing = NO;//不清空
//是否文字以圓點(diǎn)格式顯示
textField.secureTextEntry = YES;//密碼模式textField.secureTextEntry = NO;//普通模式
//鍵盤右下角return按鈕類型(枚舉值)
textField.returnKeyType = UIReturnKeyNext;
//自定義輸入視圖(默認(rèn)是鍵盤)
textField.inputView = myInputView;
//輸入視圖上方的輔助視圖(默認(rèn)nil)
textField.inputAccessoryView = myAccessoryView;
   [self.view  addSubview:textField];
   [textField release];//釋放內(nèi)存管理

UITextField 外觀控制屬性

UITextField 常用代理方法

三、UIButton使用示例

 // 便利構(gòu)造器方法創(chuàng)建對(duì)象
   UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
   loginButton.frame = CGRectMake(30, 200, 60, 30);
// 設(shè)置button的標(biāo)題
   [loginButton setTitle:@"登錄" forState:UIControlStateNormal];
   // 添加點(diǎn)擊事件
   [loginButton addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
   [containerView addSubview:loginButton];

UIButtonc 外觀控制屬性


UIButton 添加事件

四、UIImageView(于顯示圖片的類)

UIImageView *imageView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"BackGround"]]; 
imageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self.view addSubview: imageView];

實(shí)現(xiàn)僵尸行走動(dòng)畫

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *imageView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"BackGround"]];
    imageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self.view addSubview: imageView];
       //[self.view addSubview:imageView];
    
    NSMutableArray * imageArray = [NSMutableArray array];
    for (int i = 0; i<=21; i++) {
        NSString * imageName = [NSString stringWithFormat:@"Zombie%d.tiff",i];
        UIImage * image = [UIImage imageNamed:imageName];
        [imageArray addObject: image];
        
    }
   // NSLog(@"%@",imageArray[1]);
    UIImageView *ZombieImageView = [[UIImageView alloc]initWithImage:[imageArray firstObject]];
    ZombieImageView.frame = CGRectMake(100, 200, 200, 150);
    
    [self.view addSubview:ZombieImageView];
    //設(shè)置要播放的一組圖片
    ZombieImageView.animationImages = imageArray;
    //設(shè)置播放一組圖片的總時(shí)長
    ZombieImageView.animationDuration = 3;
    //設(shè)置播放一組圖片的循環(huán)次數(shù)(0位無限循環(huán))
    ZombieImageView.animationRepeatCount =0;
    [ZombieImageView startAnimating];
    
    NSMutableArray * imageArray1 = [NSMutableArray array];
    for (int j = 1; j<=18; j++) {
        NSString * imageName = [NSString stringWithFormat:@"flower%d.tiff",j];
        UIImage * image = [UIImage imageNamed:imageName];
        [imageArray1 addObject: image];
        
    }
    
    UIImageView * flowerView = [[UIImageView alloc]initWithImage:[imageArray1 firstObject]];
    flowerView.frame = CGRectMake(400, 300, 100, 100);
    [self.view addSubview:flowerView];
    flowerView.animationImages = imageArray1;
    flowerView.animationDuration = 2;
    flowerView .animationRepeatCount = 0;
    [flowerView startAnimating];
   [self animation:flowerView duration:5.0f frame:CGRectMake(-100, 400, 100, 100)];
    
    
 //平移動(dòng)畫
//    [UIView animateWithDuration:5.f animations:^{
//        ZombieImageView.frame = CGRectMake(-200, 100, 200, 200);
//    } completion:^(BOOL finished) {
//        [UIView animateWithDuration:5.0f animations:^{
//             ZombieImageView.frame = CGRectMake(100, 200, 200, 150);
//        }];
//    }];
    [self animation:ZombieImageView duration:5.0f frame:CGRectMake(-200, 200, 200, 150)];
}
- (void)animation:(UIView *)animationView duration:(NSTimeInterval)duration frame:(CGRect)frame {
    [UIView animateWithDuration:duration animations:^{
        animationView.frame = frame;
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,044評(píng)論 4 61
  • 正如書內(nèi)封聲稱的“國內(nèi)心理學(xué)家系統(tǒng)透視中國國民性”一樣,它分析了中國式好人、中國人的情感模式、孝順等,尤其是大部分...
    giguala閱讀 480評(píng)論 1 0
  • 我的那只貓絕不像上圖中的貓小姐們那般“貴氣”,自我遇見它那天起它就是一副邋遢的“小乞丐”模樣,一身臟兮兮的灰黑色雜...
    莒語閱讀 402評(píng)論 0 0
  • 拈花悟道一笑之, 成佛成魔在咫尺, 人杰鬼雄生世間, 逍遙自在只在心。
    李不白_閱讀 228評(píng)論 0 0
  • 有多少贊美就有多少批評(píng),有多少微笑就有多少淚水。 ——席鏡明
    席鏡明閱讀 118評(píng)論 0 0

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