seguedestination
seguedestination

Reputation: 419

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: enter image description here

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 : enter image description here

My storyboard design is: enter image description here

enter image description here

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

Upvotes: 0

Views: 658

Answers (1)

Pramod Tapaniya
Pramod Tapaniya

Reputation: 1266

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

Upvotes: 1

Related Questions