iOS階段學習第26天筆記(UILabel的介紹),第26天uilabel

來源:互聯網
上載者:User

iOS階段學習第26天筆記(UILabel的介紹),第26天uilabel

iOS學習(UI)知識點整理

一、關於UILabel的使用介紹

1)概念:UILabel是一個繼承自UIView的用於展示文本資訊的控制項

2)UI中所有的控制項都繼承自UIView 即UIView 是UI的祖宗類。

3)UILable的執行個體化方式 代碼:

1  UILabel *label=[[UILabel alloc]init]; //初始化UILabel     2  label.text=@"Hello,KingKong";//給label賦值常值內容3  label.backgroundColor=[UIColor blackColor];//設定lable背景顏色為黑色4  [label setTextColor:[UIColor whiteColor]];//設定文本字型顏色為白色5   label.font=[UIFont systemFontOfSize:15.0];//設定label字型大小6  abel.frame=CGRectMake(20, 60, 200, 40);//設定label的x、y座標以及長度  和高度7  [self.window addSubview:label];//將初始化好的label裝載到螢幕視圖

 

4)視圖顏色可以根據RGB類型設定,使用取色軟體擷取我們想要的顏色的RGB值可以方便的設定任意顏色類型
     為了使用方便我們可以 寫一個專門的根據RGB設定顏色的宏定義方法 代碼如下:

1  #define ColorWithRGB(r,g,b) ([UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1])

 
5)我們還可通過NSMutableAttributedString 設定控制項的樣式 執行個體代碼:

 1  UILabel *label=[[UILabel alloc]init]; //初始化UILabel    2  //初始化NSMutableAttributedString對象 3  NSMutableAttributedString *attribute=[[NSMutableAttributedString alloc]initWithString:text]; 4 //根據範圍設定字型大小 5 [attribute addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30] range:NSMakeRange(0, 1)]; 6 //根據範圍設定背景顏色 7 [attribute addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(0, 1)]; 8 //根據範圍設定字型顏色 9 [attribute addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 1)];10  //根據label的寬度自動調節文字的字型大小11   label.adjustsFontSizeToFitWidth = YES;12 label.numberOfLines = 0;//最大行數直到內容把label寬度高度填充完全13  label.lineBreakMode = NSLineBreakByTruncatingTail;14  //根據RGB設定文字顏色 此處為自訂方法15  label.textColor = ColorWithRGB(255, 154, 155);16  //設定文字置中17  //在iOS 7 之前UITextAlignmentCenter18  label.textAlignment = NSTextAlignmentCenter;19   //設定label的邊框的寬度20  label.layer.borderWidth = 1;21  //設定邊框的顏色22  label.layer.borderColor = label.textColor.CGColor;23  //設定圓角 (當映像是正方形的時候,圓角值為映像一半的時候,映像變為一個圓)24   label.layer.cornerRadius = 6;25  //label文字的陰影26   label.shadowColor = [UIColor blackColor];27  //laebl文字的位移量28  label.shadowOffset = CGSizeMake(10, 10);29 //將設定好的樣式賦值給控制項30 label.attributedText=attribute;31 //裝載控制項32 [self.window addSubview:label];

 
6)當我們不使用iOS的Main.storyboard 而使用自訂的視圖頁面時需要 刪除Target 下 Info選項的Main storyboard file base name
      否則會執行報錯

7)當我們沒法選擇工程運行所需的iOS模擬器是 在Target的General下找到Deployment Target 項設定一下對應的iOS版本即可

8)iOS UI項目建立選項一般為 Single View Application 項

9)我們在初始化控制項對象時必須先初始化視圖 代碼如下: 

1 //初始化視圖對象  [UIScreen mainScreen].bounds  自動擷取螢幕的尺寸2 self.window = [[UIWindow alloc] initWithFrame:[UIScreenmainScreen].bounds];   3 //設定視圖背景顏色4 self.window.backgroundColor = [UIColor whiteColor];5 //把當前的window設定成主window,並把它展示出來 註:此項必須設定否則無法展示視圖6 [self.window makeKeyAndVisible];

 

10)什麼是Frame? Frame是座標系,第一個參數是x座標,第二個參數是y座標,第三個參數是寬度,第四個參數是高度 

1 CGRect frame = CGRectMake(10, 0, 100, 50);

 
11) 擷取螢幕或視圖的寬\高、x座標,y座標 

 1 CGRect mainScreenFrame = [UIScreen mainScreen].bounds; 2 //擷取螢幕寬度 3 CGFloat screenWidth1 = mainScreenFrame.size.width; 4 //擷取視圖的寬度 5 CGFloat screenWidth2 =CGRectGetWidth(self.view.frame); 6 //擷取螢幕的高度 7 CGFloat screenHeight1 = mainScreenFrame.size.height; 8 //擷取視圖的高度 9 CGFloat screenHeight2 =CGRectGetHeight(self.view.frame);10 //擷取螢幕的X座標11 CGFloat startX = mainScreenFrame.origin.x;12 //擷取螢幕的Y座標13 CGFloat startY = mainScreenFrame.origin.y;

12)PCH檔案說明:  PCH檔案是iOS中的先行編譯檔案可以在裡面實現一些對標頭檔的引用以及宏定義方法,
        這樣就可以實現一個全域引用 ,在添加PCH檔案後需要在Target的 Build Settings 項下找到
        Prefix  Header  填寫 $(SRCROOT)/Target名稱/PCH檔案名稱.pch 

13)建立控制項的必要流程
  1、控制項初始化
  2、設定Frame
  3、設定背景顏色
  4、將控制項載入在某一視圖上
  可選
  * 可否設定文字?
  * 可否設定文本顏色?
  * ……

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.