문제

I'm using the following code to auto adjust the height of a label in a UITableView. It works the majority of the time, but certain times text is cut off. Is there something wrong with my code, or anything else I need to add?

UILabel *textLabel = ((UILabel *)[cell viewWithTag:3]);
textLabel.text = text;

CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);

CGSize expectedLabelSize = [text sizeWithFont:textLabel.font constrainedToSize:maximumLabelSize lineBreakMode:textLabel.lineBreakMode];

//adjust the label the the new height.
CGRect newFrame = textLabel.frame;
newFrame.size.height = expectedLabelSize.height;
textLabel.frame = newFrame;
도움이 되었습니까?

해결책

In iOS 7 sizeWithFont: constrainedToSize: lineBreakMode: is deprecated, now you should use:

 CGSize maxSize = CGSizeMake(296.f, FLT_MAX);
 CGRect labRect = [someText boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:textLabel.font} context:nil];


 textLabel.frame = CGRectMake(0, 0, maxSize.width, labRect.size.height);
 textLabel.text = someText;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top