iosiOSlabel基本使用以及文字自適應

來源:互聯網
上載者:User

標籤:

(如果需要的不是使用的屬性值如換行形式,可以把對應的屬性在程式中書寫然後按"command"+滑鼠左鍵點擊就可以查看所有屬性值)

一label基本設定

self.view.backgroundColor = [UIColor redColor];

    //建立第一個標籤控制項

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 200, 30)];

    //對位置設定

    //對控制項的中心點進行設定

    label.center = self.view.center;

    label.frame = CGRectMake(20, 20, 30, 30);

    

    //顯示文字

    label.text = @"我是美女";

    

    //設定字型大小

    label.font = [UIFont systemFontOfSize:30];

    

    //自適應大小的方法   標籤的大小由字型的大小長度決定

    [label sizeToFit];

    

    //字型的顏色  alpha 透明度 0 - 1   0- 1

    label.textColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:195/255.0 alpha:1];

    

    //Red Green Blue 0 - 255   255  255  255

    //  0 - 1

    //字型對齊格式  右側是枚舉類型

    label.textAlignment = NSTextAlignmentCenter;

    

    //加背景顏色

    label.backgroundColor = [UIColor greenColor];

    

    //顯示出來 將標籤 放到視圖上 進行顯示

    

    [self.view addSubview:label];

    //addSubview 添加子視圖

    

    //不是程式崩潰前提下 問題:

    //第一點  frame是否設定了

    //第二點  是不是加到了父視圖中

    //第三點  背景色和 控制項顏色 一樣

 

二.文字自適應

//建立label

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, 200, 999)];

    label.backgroundColor = [UIColor greenColor];

    label.text = @"To be or not to be, that is a question。To be or not to be, that is a question。To be or not to be, that is a question。To be or not to be, that is a question。To be or not to be, that is a question。";

    label.font = [UIFont systemFontOfSize:18];

    label.textColor = [UIColor redColor];

   //設定 label的換行模式

    label.lineBreakMode = NSLineBreakByWordWrapping; //根據單詞進行換行

    //設定label顯示幾行  可以有無限行

    label.numberOfLines = 0;

 

    [label sizeToFit];

    

    [self.view addSubview:label];

iosiOSlabel基本使用以及文字自適應

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.