-[NSString sizeWithFont:] returns different results in release and debug builds

1.6k Views Asked by At

I've used this code to resize a UILabel's frame height to fit dynamic text several times with success:

(...)
CGSize labelSize = [thelabel.text sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

All was well until I started to build my apps using the iOS5 SDK.

For armv6 devices (iPhone3G/-), when building for debug, the label size is okay, but when building for release, the height value retrieved is the same as the width. I am running both of these builds on an actual device.

example with following log line:

NSLog(@"labelSize: %f %f", labelSize.width, labelSize.height);

output: iphone3G - debug >

Thu Nov 3 18:22:50 unknown appname[1071] : labelSize: 115.000000 19.000000

iphone3G - release >

Thu Nov 3 18:22:50 unknown appname[1071] : labelSize: 115.000000 115.000000

Can anyone help or provide me with another solution to adjusting UILabel's height based on the text length?

I'm aware there are some similar questions about this method, but they do not address this particular issue.

3

There are 3 best solutions below

0
On

I still haven't managed to understand the problem.

Although, replaced the UILabel resize method with an alternative way:

//init and store initial rect
CGRect initialLabelRect = CGRectMake(2*kMargin, auxHeight , 200, 0 /*will be overriden*/);
UILabel *dataLabel = [[UILabel alloc] initWithFrame:initialLabelRect];
dataLabel.text = @"long text here";
[dataLabel setFont:[UIFont fontWithName:@"Arial-BoldMT" size:15]];          
//fit size will adjust both width and height, but i'll be discarding the width
[dataLabel sizeToFit];
//set the new frame (initial, but with the computed height)
dataLabel.frame = CGRectMake(initialLabelRect.origin.x, initialLabelRect.origin.y, initialLabelRect.size.width, dataLabel.frame.size.height);

//add and release 
[superview addSubview:dataLabel];
[dataLabel release];

So, the issue persists, but, for the time being... I'm replacing the code occurrences for this approach.

If someone come up with a clue about what happened, please let me know. I'm guessing it+s a bug on sizeWithFont method... but is always easier to blame on the SDK when we can't understand... :)

Thank you all. Cheers.

0
On

Had the same issue.

If you follow the link supplied by vfonseca, you arrive at: Is there a way to compile for ARM rather than Thumb in Xcode 4?

And the selected answer tells you how to add the correct compiler flags to prevent this. Alternatively upgrade to Xcode 4.3 which has fixed this bug.

0
On

I've discovered a difference in what sizeWithFont:constrainedToSize:lineBreakMode: will return depending on which architecture your code is running on. I've used the function to calculate the height of strings I want to display in UILabels in UITableViewCells so that I can calculate the final height of the cell. I was getting one size on my iPhone 4, and a tester was getting big large cells on their iPod (old armv6 device).

The solution was posted here: Is there a way to compile for ARM rather than Thumb in Xcode 4?

I'm sure the optimization fix described below works as well, but disabling thumb might allow you to maintain your optimization settings? I don't know, you'd have to test this.