我的iOS學習歷程 - UIView和UILabel

今天主要說的是UIView和UILabel的屬性和應用

首先我將今天講的方法總結在下面

UIView

1 初始化一個UIView,起始點 從屏幕的左上角(0,0)點 開始計算
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];

2 加背景顏色
view.backgroundColor = [UIColor redColor];

3 添加到window上面,實際上添加進一個數組中
[self.window addSubview:view];

4 取出子視圖
NSArray *subView = self.window.subviews;

5 獲取中心點(x = frame.origin.x + frame.size.width / 2,y同x)
CGPoint center = view.center;
NSLog(@”%@”,NSStringFromCGPoint(center));

6 更改中心點座標
view.center = CGPointMake(200, 300);

7 bounds 邊界 可以控制子視圖的座標系,默認bounds的起始點 就是從(0,0)開始
改變父視圖的bounds 相當於改變子視圖的座標系(也就是原點座標),父視圖是不發生變化 只是更改子視圖的位置
view.bounds = CGRectMake(0, 100, 200, 100);

8 添加子視圖(必須把子視圖添加到父視圖的範圍之內否則無法交互)
[view addSubview:view1];

視圖屬性

9 隱藏視圖( 如果父視圖被隱藏 那麼父視圖上面所有子視圖也會被隱藏)
aView.hidden = NO;// YES爲顯示 NO爲隱藏

10 透明屬性 (如果父視圖的透明度發生變化 那麼上面的所有子視圖都會發生變化)
aView.alpha = 0.5

11 取出某一子視圖的父視圖
UIView *superView = bView.superview;

12 添加一個標籤tag值(重點) 給視圖加標記 然後可以用viewWithTag取出(不能給0)
aView.tag = 10;
通過tag值取出視圖
UIView *tView = [self.window viewWithTag:10];

13 把視圖插入到指定的位置
[self.window insertSubview:bView atIndex:0];

14在指定的視圖上面添加子視圖
[self.window insertSubview:bView aboveSubview:aView];

15 在指定的視圖下面添加子視圖
[self.window insertSubview:bView belowSubview:aView];

16 把某一個子視圖放在最前面
[self.window bringSubviewToFront:aView];

17 把某一個子視圖放在最後面
[self.window sendSubviewToBack:aView];

18 交換兩個子視圖的位置
[self.window exchangeSubviewAtIndex:0 withSubviewAtIndex:1];

19 從父視圖上刪除
[bView removeFromSuperview];

UILabel

20 顯示文本的控件label初始化
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
label.backgroundColor = [UIColor yellowColor];

21 添加字
label.text = @”Application windows are expected to have a root view”;

22 設置字體對齊方式(0: 左對齊 1: 居中 2: 右對齊)
label.textAlignment = 0;

23 設置字體顏色
label.textColor = [UIColor redColor];

24 設置字體字號(大小)
label.font = [UIFont systemFontOfSize:18];

25 獲取系統所有安裝的字體
NSArray *fontArray = [UIFont familyNames];

26 設置字體類型
label.font = [UIFont fontWithName:@”Apple Color Emoji” size:12];

27 設置陰影顏色
label.shadowColor = [UIColor blackColor];

28 調整陰影的位置 (y正值陰影向下偏 x正值向右偏)
label.shadowOffset = CGSizeMake(0, 15);

29 多行顯示
完全顯示 填-1 或者 0;超出範圍就無法顯示了
label.numberOfLines = 0;

30 設置斷行模式
label.lineBreakMode = 5;

UIView例題:

    //  UIView 代表一個矩形區域
    //  初始化一個UIView
    //  起始點 從屏幕的左上角(0,0)點 開始計算
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
    //  加背景顏色
    view.backgroundColor = [UIColor redColor];
    //  添加到window上面
    [self.window addSubview:view];

UILabel例題:(由於沒有學習圖片 暫時用label代替)

UILabel *movieIconLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 100, 200)];
    movieIconLabel.backgroundColor = [UIColor grayColor];
    movieIconLabel.text = @"敢死隊";
    movieIconLabel.textAlignment = 1;
    [self.window addSubview:movieIconLabel];
    [movieIconLabel release];

    UILabel *ratingLabel = [[UILabel alloc]initWithFrame:CGRectMake(150, 20, 100, 30)];
    ratingLabel.backgroundColor = [UIColor whiteColor];
    ratingLabel.text = @"評分: 7.1";
    [self.window addSubview:ratingLabel];
    [ratingLabel release];

    UILabel *rating_countLabel = [[UILabel alloc]initWithFrame:CGRectMake(270, 20, 100, 30)];
    rating_countLabel.backgroundColor = [UIColor whiteColor];
    rating_countLabel.text = @"(4925評論)";
    [self.window addSubview:rating_countLabel];
    [rating_countLabel release];

    UILabel *dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(150, 60, 100, 30)];
    dateLabel.backgroundColor = [UIColor whiteColor];
    dateLabel.text = @"20140901";
    [self.window addSubview:dateLabel];
    [dateLabel release];

    UILabel *runtimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(150, 110, 100, 30)];
    runtimeLabel.backgroundColor = [UIColor whiteColor];
    runtimeLabel.text = @"126 min";
    [self.window addSubview:runtimeLabel];
    [runtimeLabel release];

    UILabel *genresLabel = [[UILabel alloc]initWithFrame:CGRectMake(150, 160, 150, 30)];
    genresLabel.backgroundColor = [UIColor whiteColor];
    genresLabel.text = @"動作/冒險/驚悚";
    [self.window addSubview:genresLabel];
    [genresLabel release];

    UILabel *countryLabel = [[UILabel alloc]initWithFrame:CGRectMake(150, 200, 100, 30)];
    countryLabel.backgroundColor = [UIColor whiteColor];
    countryLabel.text = @"美國|法國";
    [self.window addSubview:countryLabel];
    [countryLabel release];

    UILabel *makeNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 220, 100, 50)];
    makeNameLabel.backgroundColor = [UIColor whiteColor];
    makeNameLabel.text = @"製作人";
    makeNameLabel.font = [UIFont systemFontOfSize:24];
    [self.window addSubview:makeNameLabel];
    [makeNameLabel release];

    UILabel *actorsLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 280, 350, 80)];
    actorsLabel.backgroundColor = [UIColor whiteColor];
    actorsLabel.text = @"西爾維斯特•史泰龍 Sylvester Stallone,傑斯•斯坦森 Jason Statham,梅爾•吉布森 MelGibson,李連杰 Jet Li";
    actorsLabel.numberOfLines = 0;
    [self.window addSubview:actorsLabel];
    [actorsLabel release];

    UILabel *moviePlotLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 350, 150, 40)];
    moviePlotLabel.backgroundColor = [UIColor whiteColor];
    moviePlotLabel.text = @"電影情節";
    moviePlotLabel.font = [UIFont systemFontOfSize:24];
    [self.window addSubview:moviePlotLabel];
    [moviePlotLabel release];

    UILabel *plotLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 380, 350, 300)];
    plotLabel.backgroundColor = [UIColor whiteColor];
    plotLabel.text = @"在第敢死隊3 劇照敢死隊3 劇照 (50張)三部的故事中,巴尼(西爾維斯特·史泰龍飾)與克里斯馬斯(傑森·斯坦森飾)領銜的敢死隊將正面迎戰昔日戰友、如今的軍火梟雄康拉德·斯通班克斯(梅爾·吉布森飾)。斯通班克斯曾僥倖死裏逃生過一次,他對敢死隊下達了絕殺令巴尼則另有一番打算:爲迎戰強敵,巴尼決定給敢死隊注入新鮮血液,招募了更快更強的高科技戰鬥新生,搭配長槍硬炮的硬漢前輩,展開一番大決戰.";
    plotLabel.numberOfLines = 0;
    [self.window addSubview:plotLabel];
    [plotLabel release];

結果展示:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章