Objective-c – How to fit a text in UILabel when the size is not proportionally

cocoa-touchiphoneobjective cuilabel

As per the title, how would I fit text into a UILabel when the width and height are not proportional?

In my application, the label has a width of 100, height of 500, and font size of 400. The result in the simulator is that the text is out of frame and cannot completely display on-screen. If I enable adjustsFontSizeToFitWidth the font is no longer 400, which is a requirement.

I know if the text does not scale proportionately it will look odd but that's not a problem for me.

Best Answer

If you just want to have the label be as large as it needs to be to contain the label, you can set it to larger than it needs to be in any circumstance and align it as necessary.

Another option is this:

label.text = newText;
CGRect bounds = label.bounds;
bounds.size = [newText sizeWithFont:label.font];
label.bounds = bounds;