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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.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 12-29-2008, 11:33 PM   #1 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 4,814
Default How to calculate height of cell to contain text?

I am creating a UITableView that will be used to display cells that contain, among other things, a UITextView. (or possibly a UILabel)

The contents of the text view will vary from cell to cell. I want to make each cell tall enough to display the text in it's text view.

How do I calculate how tall my text field needs to be in order to display the text I want to display? (at the current font and size for the view.)
Duncan C is offline   Reply With Quote
Old 12-30-2008, 12:33 AM   #2 (permalink)
Registered Member
 
mnemonic_fx's Avatar
 
Join Date: Jun 2008
Posts: 419
Default

Code:
NSString *string = ...;

CGSize stringSize = [string sizeWithFont:[UIFont boldSystemFontOfSize:kTextFieldFontSize] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:UILineBreakModeWordWrap];
Then you could use the stringSize however you want, the key is in 9999 as a height.
You would rarely find any text view rect with more than 9999 as a height. Unless if it's an article or something.

Currently, I can not find any way to calculate it without constraint.

Last edited by mnemonic_fx; 12-30-2008 at 12:37 AM.
mnemonic_fx is offline   Reply With Quote
Old 01-03-2009, 12:26 AM   #3 (permalink)
see my iOS apps! :D
 
Join Date: Sep 2008
Location: Europe
Posts: 280
Default works almost perfectly

This routine do not works very well, probably due to some bug.

The calculated frame size is not correct when you have letters that go below the baseline. The string aaaaa will be fine, but not aapqg, because qpg have pixels below the base line.
LunarMoon is offline   Reply With Quote
Old 01-03-2009, 10:01 AM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 4,814
Default sizeWithFont: constrainedToSize: errors

Quote:
Originally Posted by LunarMoon View Post
This routine do not works very well, probably due to some bug.

The calculated frame size is not correct when you have letters that go below the baseline. The string aaaaa will be fine, but not aapqg, because qpg have pixels below the base line.
Oh great.

What are the errors like? Does the frame size it gives you clip the descenders of the characters on the bottom row?

I haven't looked into fonts in Cocoa, but it should be possible to query the font for the leading value at the current size and style, and round the answer you get back up to the next multiple of the total font height (height + leading). Even simpler, you could just add myfont.descender to the height you get back from the call to sizeWithFont: constrainedToSize:.


Duncan C
Duncan C is offline   Reply With Quote
Old 01-03-2009, 11:19 AM   #5 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

Typically you want table rows to be a minimum of 44 high. Also, I find I usually add some margin also. If LunarMoon is correct I've noticed this problem since I'm always adding at least 20 pixels to the height anyway.
PhoneyDeveloper is offline   Reply With Quote
Old 01-03-2009, 02:55 PM   #6 (permalink)
see my iOS apps! :D
 
Join Date: Sep 2008
Location: Europe
Posts: 280
Default

This is also my case. I am adding exactly 20 pixels too!!!! If you want to see the bug just add the text to the UITextView programatically as

Code:
[box setText: @"apqgbADF"];
and then use the code by mnemonic_fx

To see the full effect paint the box yellow before using the code or make it clip on the boundary. All letters with pixels below the baseline will be cut, i.e., it will clip the descenders of the characters on the bottom row.

I cannot believe Apple left behind something so simple and so basic as centering a &%$&#%$&% text vertically! We have to build 10,000 lines of code using sometimes buggy code to center a text on a box.

This remembers me the nightmare you have to pass to center a /&%%%#/ text horizontally and vertically (in all browsers) using CSS in HTML? What is wrong with these guys creating programming languages and standards? They build cars but always forgot to put the steering wheels.
LunarMoon is offline   Reply With Quote
Old 01-03-2009, 05:18 PM   #7 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

My tableviews have UILabels in them. So the bug you describe is with UITextView? I guess that's why I haven't seen it.
PhoneyDeveloper is offline   Reply With Quote
Old 01-04-2009, 02:58 AM   #8 (permalink)
see my iOS apps! :D
 
Join Date: Sep 2008
Location: Europe
Posts: 280
Default

yes, with UITextView.
LunarMoon is offline   Reply With Quote
Old 01-04-2009, 10:43 AM   #9 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 123
Question

Quote:
Originally Posted by LunarMoon View Post
I cannot believe Apple left behind something so simple and so basic as centering a &%$&#%$&% text vertically! We have to build 10,000 lines of code using sometimes buggy code to center a text on a box.
...this from the guys that created enough NSString methods to strangle a Clydesdale

Ahh... one must understand the Apple "mindset"...

1) The impossible... we deliver!
2) The difficult... we make easy!
3) The basic... we ignore!

from 30 years experience dealing with Apple

HTH

Dick
dicklacara is offline   Reply With Quote
Old 01-04-2009, 02:25 PM   #10 (permalink)
see my iOS apps! :D
 
Join Date: Sep 2008
Location: Europe
Posts: 280
Default

unbelievable.... and BTW this Objective-C language oficially used to develop the applications is the most insane and complex language I ever used. I can understand the language without problems but I have sanity to see it very complex and not open to the everyone.

something like...
Code:
if (x == "string")
becomes
Code:
if ([x isEqualToString:@"string"])

or...
Code:
x = [10, 20, 30];
becomes
Code:
x = [[[NSMutableArray alloc] initWithObjects:
	   [[NSNumber alloc] initWithInteger:10],
	   [[NSNumber alloc] initWithInteger:20],
	   [[NSNumber alloc] initWithInteger:30],
	   nil] retain];
is insanity at its best... why easy things? what Apple has to gain? more developers? more users? pfuuuuu
LunarMoon is offline   Reply With Quote
Reply

Bookmarks

Tags
resizing, text, uitableviewcell

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: 235
16 members and 219 guests
ADY, Alsahir, cacao, dacapo, Dani77, Desert Diva, djohnson, F_Bryant, HemiMG, jansan, M@realobjects, MarkC, prchn4christ, smethorst, spiderguy84
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,228
Posts: 380,762
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

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