Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize UILabel based on content

I have a UILabel, his text size has the property

title.adjustsFontSizeToFitWidth = YES;

that prevents me from using standard methods to resize the UILabel. I read on another post here that I'm supposed to use the function

sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode

from this answer: How to figure out the font size of a UILabel when -adjustsFontSizeToFitWidth is set to YES?

Now, i can't figure out how to make it work.. this is the actual code

UIFont *font = [UIFont fontWithName:@"Marker Felt" size:200];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, width, 20.0)];
title.text = @"this is a long title that should be resized";
title.font = font;
title.numberOfLines = 1;
title.adjustsFontSizeToFitWidth = YES;

CGFloat pointSize = 0.0;
CGSize size = [title.text sizeWithFont:font 
                           minFontSize:title.minimumFontSize 
                        actualFontSize:&pointSize 
                              forWidth:width 
                         lineBreakMode:title.lineBreakMode];
title.frame = CGRectMake(title.frame.origin.x, 
                         title.frame.origin.y, 
                         size.width, 
                         size.height);

The UILabel get resized wrongly, as if the font size it's still 200.. Any clues? Thanks!

like image 263
ksn Avatar asked Aug 30 '11 11:08

ksn


2 Answers

I have some code you could use on my github, check it out, it's a category for UILabel, you need to set the frame width and when you can resizeToFit on the UILabel, it adjusts the height to fit the content, and returns the y position of the end of the label so you can adjust any content appearing after it.

https://gist.github.com/1005520

like image 128
Daniel Avatar answered Nov 02 '22 12:11

Daniel


I'd suggest filing this as a bug.

The size returned by -sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: has the correct width, but not the height does not account for the actual font size.

It seems likely that UILabel also has this bug. Changing the size of a label to match the height of the text in a font of the size returned by -sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: will incorrectly vertically position the text within the label.

A work-around is to calculate the correct height, and change the font on the label to with the actual font size:

CGFloat pointSize = 0.0f;
CGRect frame = title.frame;
frame.size = [title.text sizeWithFont:font
                          minFontSize:title.minimumFontSize
                       actualFontSize:&pointSize
                             forWidth:width
                        lineBreakMode:title.lineBreakMode];
UIFont *actualFont = [UIFont fontWithName:@"Marker Felt" size:pointSize];
CGSize sizeWithCorrectHeight = [title.text sizeWithFont:actualFont];
frame.size.height = sizeWithCorrectHeight.height;
title.frame = frame;
title.font = actualFont;
like image 25
lemnar Avatar answered Nov 02 '22 10:11

lemnar