CGSize sizeWithFont does not return the correct height

I have a UILabel that wraps words correctly if I make its frame height high enough. However, I want it to dynamically find the desired height, and the method sizeWithFontdoes not; this method just creates a single line in my label:

CGSize subtitleSize=[subtitle sizeWithFont:subtitleFont forWidth:maxwidth lineBreakMode:UILineBreakModeWordWrap];

Then I use the same text, font and size from this function:

        UILabel *subLabel=[[[UILabel alloc] initWithFrame:CGRectMake(0, 0, maxwidth, subtitleSize.height)]autorelease];
        subLabel.font=subtitleFont;
        subLabel.text=subtitle;
        subLabel.numberOfLines=0;
        subLabel.lineBreakMode=UILineBreakModeWordWrap;

It seems like everything should work and give me the size that I then use. But the height of the size is always equal to one line for the label.

+3
source share
3 answers

you can use this

- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode; 

max size in constrainedToSize

CGSize size = CGSizeMake(maxwidth, CGFLOAT_MAX);
+7
source

-, , , . :

CGSize subtitleSize = [subtitle sizeWithFont:subtitleFont constrainedToSize:CGSizeMake(maxwidth, 1000000) lineBreakMode:UILineBreakModeWordWrap];

"" .

, .

+1

You can increase the font size manually, which worked for me.

+1
source

All Articles