简体   繁体   中英

How can I adjust UILabel height to fit the size?

I know for IO7 and later , boundingRectWithSize can do the work. But in order to fit all the iOS version. I use sizeThatFits to calculate.Following is my code:

 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; 

And result is: 在此处输入图片说明

the last two word "provides trusted" is missing!!!!!!

How ever when i append some word

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";

The result is : 在此处输入图片说明

My storyboard design is: 在此处输入图片说明

在此处输入图片说明

I guess something wrong with the line break.. when the next line text is not long enough the next line is miss..

Try this code

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM