Reputation: 419
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..
Upvotes: 0
Views: 658
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