天天看點

iOS個人整理02-UILabel/UIIamgeView

昨天學習了工程的建立和UIView視圖的簡單使用,今天總結一些最基本的控件

一、UILabel

标簽,很簡單,顯示文字的

下面是UILabel的各種屬性

//初始化設定大小,有自己的初始化方法就用自己的,沒有就用父類的
    UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10,250, 150)];
    //設定Label的行數,前提是Frame高度要夠,當設定為0,文字會根據高度自動折行
    myLabel.numberOfLines = 0;
    //設定label背景色
    [myLabel setBackgroundColor:[UIColor greenColor]];
    myLabel.text = @"He that would command must serve.";
    //設定字型的顔色
    myLabel.textColor = [UIColor blueColor];
    //輸出系統預設的字型大小
    NSLog(@"%@",myLabel.font);
    //設定字型大小
    myLabel.font = [UIFont boldSystemFontOfSize:20];//加粗bold,有些字型不支援

    //陰影的顔色和偏移量
    myLabel.shadowColor = [UIColor brownColor];
    myLabel.shadowOffset = CGSizeMake(3, 3);
    
    //得到系統提供的所有字型
    NSArray *fonts = [UIFont familyNames];
    NSLog(@"%@",fonts);
    //myLabel.font = [UIFont fontWithName:@"Zapfino" size:15];
    
    //設定Label的折行方式
    myLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
    
    //設定文字對齊方式,居中,左對齊等等
    myLabel.textAlignment = NSTextAlignmentCenter;
    //把Label添加到window上
    [self.window addSubview:myLabel];
           

二、UIIamgeView

類似于相框,先建立好再放圖檔進去

首先拖動一個圖檔ganggang.jpg到工程目錄下

iOS個人整理02-UILabel/UIIamgeView

在代碼中添加相框和照片對象,這是圖檔最基本的應用。

在工程中不允許有兩個名字相同的圖檔,即使路徑不同

//初始化相框
    UIImageView *myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 200, 200, 300)];
    //添加相框到window
    [self.window addSubview:myImageView];
    //建立一個圖檔對象,目前目錄下的圖檔名
    UIImage *myImage = [UIImage imageNamed:@"ganggang.jpg"];

    //也可以根據路徑得到圖檔,建立一個路徑字元串
    NSString *path = [[NSBundle mainBundle]pathForResource:@"/Users/Documents/Class_02_UIButtonImage" ofType:@"jpg"];
    //根據路徑得到圖檔
    UIImage *image = [UIImage imageWithContentsOfFile:path];

    //把圖檔對象賦給相框
    [myImageView setImage:myImage];


           

運作後可以看到label和圖檔的效果。

iOS個人整理02-UILabel/UIIamgeView

三、UIView動态圖制作

通過将gif的每一幀圖放在UIImageView的動态數組裡,進行播放操作

#pragma mark 動态圖
    
    //建立一個ImageView相框
    UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(100,50, 250, 250)];
    //imageV.center = self.window.center;
    
    //将gif的每一幀放入數組中
    NSMutableArray *imageArray = [NSMutableArray array];
    for (int i = 1; i <= 20; i++) {
        UIImage *itemImage = [UIImage imageNamed:[NSString stringWithFormat:@"zc%d.tiff",i]];
        [imageArray addObject:itemImage];
    }
    
    //将一組圖檔放在imageView的動态數組裡面
    imageV.animationImages = imageArray;
    
    //設定動畫持續時間秒,所有圖檔在這個時間内播完
    imageV.animationDuration = 2;
    
    //設定循環次數并讓動畫開始,兩句順序不能颠倒
    imageV.animationRepeatCount = 1000;
    
    //操作動畫開始
    //[imageV startAnimating];
    
    //動畫未開始時設定占位圖檔
    imageV.image = [UIImage imageNamed:@"zc1.tiff"];
    
    //顯示在window上
    [self.window addSubview:imageV];
    
    //設定相框的标簽
    imageV.tag = 1000;
    
    //建立一個按鈕,控制動畫開始和結束
    UIButton *startBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [startBtn setTitle:@"開始動畫" forState:UIControlStateNormal];
    startBtn.frame = CGRectMake(0, 0, 100, 100);
    [self.window addSubview:startBtn];
    [startBtn addTarget:self action:@selector(startBtnTouch:) forControlEvents:UIControlEventTouchUpInside];
    
    

-(void)startBtnTouch:(UIButton*)sender
{
    //得到相框的标簽
    UIImageView *imageV = (UIImageView*)[self.window viewWithTag:1000];
    sender.selected = !sender.selected;
    if (sender.selected) {
        //控制動圖開始
        [imageV startAnimating];
    }
    else
        //動圖停止
        [imageV stopAnimating];
}
           

圖檔素材:不會上傳壓縮包...自己找張gif把每一幀拖出來命名

效果如圖,當然他會動

iOS個人整理02-UILabel/UIIamgeView

明天總結button的使用