Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-23-2010, 10:09 AM   #1 (permalink)
Roi
Registered Member
 
Join Date: Oct 2009
Posts: 56
Roi is on a distinguished road
Default UILabel wrong size calculations

Hi all,
I am adjusting my old apps to iPhone 4 using the simulator at the moment and I can across a very strange behavior with UILabel drawing and sizeWithFont:constrainedToSize: that I currently see only on the iPhone 4 simulator.

I am trying to show the following error text to the user:
@"Incorrect user name or password"
This text sits inside a dynamic error box that is built from three parts: top, center and bottom and therefore I calculate the size of the label so I can change the center background image frame accordingly.

Here is an example of the UILabel size calculation code:

Code:
	CGRect errorFrame = CGRectMake(40, 0, 240.0, 22.0);
	UILabel *errorlabel = [[UILabel alloc] initWithFrame:errorFrame];
	errorlabel.adjustsFontSizeToFitWidth = NO;
	errorlabel.font = [UIFont fontWithName:@"HelveticaNeue" size:16];
	errorlabel.textAlignment = UITextAlignmentLeft;
	errorlabel.numberOfLines = 0;
	errorlabel.text = @"Incorrect user name or password";
	// since only the width is fixed I will use a really large height value
	CGSize errorLabelSize = [errorlabel.text sizeWithFont:errorlabel.font constrainedToSize:CGSizeMake(240.0, 4600.0)];
	CGRect newFrame = errorlabel.frame;
	newFrame.size.height = errorLabelSize.height;
	errorlabel.frame = newFrame;
        // added so I can easily see the new frame
	errorlabel.backgroundColor = [UIColor redColor];
	[self.errorView addSubview:errorlabel];
	[errorlabel release];
When I run the code on the iPhone 3 simulator the sizeWithFont:constrainedToSize: method returns a height of 1 line and draws this error text on 1 line.
When I run the same code on the iPhone 4 simulator sizeWithFont:constrainedToSize: returns a size of (170.0, 42.0) which is needed for two lines but label itself is drawn on 1 line.
It is as if the sizeWithFont code doesn't use the same logics of the rendering code.

Since changing the error text is no option any idea how to bypass this issue or resolve it?

Thanks in advance
Roi is offline   Reply With Quote
Old 08-23-2010, 11:00 AM   #2 (permalink)
gtyt38
 
Join Date: Apr 2010
Location: Baltimore, MD, USA
Posts: 61
gtyt38 is on a distinguished road
Default

Couldn't you use a UIAlertView to display the error? That's what I did with my in-house app and it's working just fine.
__________________
No animals were injured in the creation of this post…however, a few electrons were mildly inconvenienced…
gtyt38 is offline   Reply With Quote
Old 08-23-2010, 01:58 PM   #3 (permalink)
Roi
Registered Member
 
Join Date: Oct 2009
Posts: 56
Roi is on a distinguished road
Default

Quote:
Originally Posted by gtyt38 View Post
Couldn't you use a UIAlertView to display the error? That's what I did with my in-house app and it's working just fine.
Nope, my designer specifically requested the special graphics here
Roi is offline   Reply With Quote
Reply

Bookmarks

Tags
ios4, retina, sizewithfont, uilabel

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 340
5 members and 335 guests
freewind, HemiMG, lendo, Newbie123, PlutoPrime
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,894
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:55 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0