I'm programmatically creating multi-line UILabels ([label setNumberOfLines:0];
).
The built-in sizeToFit
method of UILabel works great for 1 line UILabels, but for multi-line text, it sets the height properly, but the width is set too small, causing longer text lines to wrap.
I don't know the label width until after the user enters their text. I want to resize the labels to fit the width of the longest line of text. And per @DonMag's comment, I also want to restrict the label to not be wider than the screen.
I tried different lineBreakMode
settings but there isn't a 'nowrap' option.
I've searched SO and there are many related solutions but none that solve the problem of sizeToFit for both width and height.
Is there a way to programmatically size a multi-line UILabel to fit BOTH the width AND the height of the text?
sizeToFit
with multi-line UILabels... The idea is that you set the width of the label, and let the wrapping setting handle the line breaks, and let auto-layout handle the height change. – DonMagsizeToFit
will then take care of the height. – koen