UILabel sizeToFit malfunction
For some reason, my use of UIView’s sizeToFit method was not working as expected. What I got when I tried to send my UILabel controls through this method was that they ended up being sized down too far, and as a result text was either eliminated from the label, or an ellipsis appeared at the end. I tried just about every combination of UILineBreakMode and numberOfLines I could think of, with no combination fixing the problem.
So what I ended up doing was to just manipulate the UILabel’s frame in the table view’s cellForRowAtIndexPath method. Here is a code snippet:
if (feedIndex == SECTION_CUSTOMER_AND_JOB_INFO)
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_CUSTOMER_AND_JOB_INFO];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:@"SummaryCustomerAndJobInfoCell" owner:self options:NULL];
cell = cellCustomerAndJobInfo;
}
// the next line uses the tag property of the label as set in the nib file
UILabel *lbl = (UILabel *)[cell viewWithTag:1];
NSString *text = @"Set your multiple line\ntext to display here...";
CGSize constraint = CGSizeMake(lbl.frame.size.width, 20000.0f);
CGSize size = [text sizeWithFont:lbl.font constrainedToSize:constraint
lineBreakMode:UILineBreakModeWordWrap];
lbl.text = text;
[lbl setFrame:CGRectMake(lbl.frame.origin.x, lbl.frame.origin.y,
lbl.frame.size.width, size.height)];
return cell;
}
By the way, don’t forget to do the same kind of size calculations in your heightForRowAtIndexPath delegate method.
This past Wednesday was a day of great sadness for me, as I had to put my 15 year old schipperke to sleep. Good bye Captain Kirk, you were an ornery little cuss but we loved you anyway. :'(