[英]How can I adjust UILabel height to fit the size?
我知道對於IO7及更高版本, boundingRectWithSize
可以完成工作。 但是為了適合所有的iOS版本。 我使用sizeThatFits進行計算,下面是我的代碼:
self.detailLabel.text=@"Visit BBC News for up-to-the-minute news, breaking news, video, audio and feature stories. BBC News provides trusted"; self.detailLabel.lineBreakMode = NSLineBreakByWordWrapping; CGSize size =[self.detailLabel sizeThatFits:CGSizeMake(287, MAXFLOAT)]; //287 is the label's width I set in storyboard [self.detailLabel setFrame:CGRectMake(self.detailLabel.frame.origin.x, self.detailLabel.frame.origin.y, self.detailLabel.frame.size.width, size.height)]; self.detailLabel.layer.borderColor=[[UIColor blackColor] CGColor]; self.detailLabel.layer.borderWidth=1;
結果是:
最后兩個詞“提供受信任”丟失了!
我何時追加一些字
self.detailLabel.text=@"Visit BBC News for up-to-the-minute news, breaking news, video, audio and feature stories. BBC News provides trusted World and";
結果是:
我的情節提要設計是:
我猜換行符有問題..當下一行文本不夠長時,下一行會丟失。
試試這個代碼
self.detailLabel.text=@"Visit BBC News for up-to-the-minute news, breaking news, video, audio and feature stories. BBC News provides trusted";
self.detailLabel.numberOfLines=0;
self.detailLabel.lineBreakMode=NSLineBreakByWordWrapping;
[self.detailLabel sizeToFit];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.