3

I want to calculate the height of a tableviewcell according to its text. I'm using

CGSize userInputSize = [userLabel sizeWithFont:[UIFont fontWithName:@"Arial" size:18.0f] forWidth:[tableView frame].size.width-10 lineBreakMode:NSLineBreakByWordWrapping]  

but somehow the return value is always 22 (size of the font). Strange thing is that when I'm using

CGSize userInputSize = [userLabel sizeWithFont:[UIFont fontWithName:@"Arial" size:18.0f] constrainedToSize:[tableView frame].size lineBreakMode:NSLineBreakByWordWrapping];

all works fine. But I would prefer the first version, so I can easily adjust the width. Why isn't it working?

Edit: sorry for the bad name convention, but userLabel is a NSString not a label

illogic
  • 137
  • 2
  • 9

2 Answers2

3

sizeWithFont is a NSString method (UIKit additions). use:

CGSize userInputSize = [userLabel.text sizeWithFont:[UIFont fontWithName:@"Arial" size:18.0f] constrainedToSize:[tableView frame].size lineBreakMode:NSLineBreakByWordWrapping];

or

CGSize userInputSize = [userLabel.text sizeWithFont:userLabel.font constrainedToSize:[tableView frame].size lineBreakMode:NSLineBreakByWordWrapping];

See NSString UIKit Additions Reference.

EDIT:

I just tried this code:

NSLog (@"test: %@", NSStringFromCGSize([@"test" sizeWithFont:[UIFont fontWithName:@"Arial" size:18.0f]]));
NSLog (@"longer test: %@", NSStringFromCGSize([@"longer test" sizeWithFont:[UIFont fontWithName:@"Arial" size:18.0f]]));

and result is:

test: {30, 22}
longer test: {85, 22}

CGSize is a struct:

struct CGSize {
   CGFloat width;
   CGFloat height;
};
typedef struct CGSize CGSize;

So you're probably looking at size.height instead of size.width

EDIT2:

from documentation sizeWithFont:forWidth:lineBreakMode:

If the size of the string exceeds the given width, this method truncates the text (for layout purposes only) using the specified line break mode until it does conform to the maximum width; it then returns the size of the resulting truncated string.

So you'll be better of defining a maximum size (real width and a big height) and go with the:

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

Please see this answer.

Community
  • 1
  • 1
Rok Jarc
  • 18,235
  • 9
  • 64
  • 120
  • Sorry for the confusion, but userLabel is actually of NSString type. So anybody got an idea why the first one won't work while the second one does? – illogic Nov 09 '12 at 09:47
  • Can you check (with `NSLog`) a size of `[tableView frame].size`? – Rok Jarc Nov 09 '12 at 10:01
  • Or try (just to check) what `[userLabel sizeWithFont:[UIFont fontWithName:@"Arial" size:18.0f]` returns. – Rok Jarc Nov 09 '12 at 10:09
  • `[tableView frame].size` is 320 and `[userLabel sizeWithFont:[UIFont fontWithName:@"Arial" size:18.0f]` is 22 – illogic Nov 09 '12 at 10:29
  • Thanks for the answer but my problem is the height as I said in my question. I try to calculate the correct height for the cell and i thought the first method `CGSize userInputSize = [userLabel sizeWithFont:[UIFont fontWithName:@"Arial" size:18.0f] forWidth:[tableView frame].size.width-10 lineBreakMode:NSLineBreakByWordWrapping]` would return the correct height, but it's always 22 while the second method returns the correct height. I hope my question is clear now :-D – illogic Nov 09 '12 at 10:53
  • 1
    Sorry for that - it's clear now :). Yes, that method simply truncates the string if it's to wide (for one line). That's why you should use the second option. – Rok Jarc Nov 09 '12 at 11:03
  • i should start using the docu more... :/ Thanks a lot for pointing this out! :D – illogic Nov 09 '12 at 12:36
0

I think it is easier this way. After setting the "text" property of the userLabel, call this method.

[userLabel sizeToFit];

At this point, the userLabel.frame has been changed so that it fits the text with the selected font. You can use userLabel.frame.size.height to adjust your table view cell.