In the NSString UIKit Additions, you can find the following method:
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode
with this one you can define the optimal height or width of your label. For example, to get the optimal height of a UILabel, you can create a category on UILabel with the following method:
- (float)optimalHeight
{
return [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(self.frame.size.width, UINTMAX_MAX) lineBreakMode:self.lineBreakMode].height;
}
after that, you can simply change the frame of your UILabel. Don't forget to set the number of line of your label to 0.