iOS label根據顯示內容自適應大小,ioslabel

來源:互聯網
上載者:User

iOS label根據顯示內容自適應大小,ioslabel

 1 - (void)setupLabel { 2     //準備工作 3     UILabel *textLabel = [[UILabel alloc] init]; 4     textLabel.font = [UIFont systemFontOfSize:16]; 5     NSString *str = @"222222222222222222222222222222222222222222"; 6     textLabel.text = str; 7     textLabel.backgroundColor = [UIColor redColor]; 8     textLabel.numberOfLines = 0;//根據最大行數需求來設定    9     textLabel.lineBreakMode = NSLineBreakByTruncatingTail;10     CGSize maximumLabelSize = CGSizeMake(100, 9999);//labelsize的最大值11     //關鍵語句12     CGSize expectSize = [textLabel sizeThatFits:maximumLabelSize];13     //別忘了把frame給回label,如果用xib加了約束的話可以只改一個約束的值14     textLabel.frame = CGRectMake(20, 70, expectSize.width, expectSize.height);15     [self.view addSubview:textLabel];16 }

 

相關文章

聯繫我們

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