2

I use a subclass of UITableViewController. In this table I use standard-cells with style UITableViewCellStyleDefault. This style contains a textLabel. I assign some text to its text-property, and then I need the text's length (in points):

cell = [tableView dequeueReusableCellWithIdentifier:@"CellID"];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellID"];
}
cell.textLabel.text = @"some text";
textWidth = [cell.textLabel.text sizeWithFont:cell.textLabel.font].width;

NSLog(@"text width = %f",textWidth);
NSLog(@"Font = '%@'",cell.textLabel.font);

But when I execute this code, it returns a width of 0.00000:

text width = 0.000000
Font = '<UICFFont: 0x68b4a90> font-family: "Helvetica"; font-weight: bold; font-style: normal; font-size: 0px'

What must I do to get the real width of this text (that is normally displayed and who's width is not 0)

Edit:
When I insert a line with the code if (cell.font){} then I get the correct size and width:

cell = [tableView dequeueReusableCellWithIdentifier:@"CellID"];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellID"];
}
cell.textLabel.text = @"some text";

// added line:
if (cell.font){}

textWidth = [cell.textLabel.text sizeWithFont:cell.textLabel.font].width;

NSLog(@"text width = %f",textWidth);
NSLog(@"Font = '%@'",cell.textLabel.font);

displays this:

text width = 98.000000
Font = '<UICFFont: 0x68dc400> font-family: "Helvetica"; font-weight: bold; font-style: normal; font-size: 20px'

But the getter "font" for a cell is deprecated. Is there a non-deprecated method to get the fontsize/textwidth?

Hubert Schölnast
  • 8,341
  • 9
  • 39
  • 76

2 Answers2

0

Same annoying problem. This seemed to work:

[cell layoutIfNeeded];

but doesn't... quite. For some strange reason it resizes the font, from about 17 to about 20.

Use [UIFont boldSystemFontOfSize:[UIFont labelFontSize]].

Steve Rogers
  • 1,963
  • 2
  • 17
  • 25
-1

Possibly by explicitly setting the font size, you can get the width of the text Label.

CGSize textLabelSize = [textLabel.text sizeWithFont:[UIFont systemFontOfSize:17]];

Here's a link that may help: dynamic calculation of UILabel width in UITableViewCell

Hope it helps! :)

Community
  • 1
  • 1
waylonion
  • 6,866
  • 8
  • 51
  • 92
  • I don't want to use a fix size. What if Apple creates a new iPhone with a new resolution and the textLabel's default size is not 17? – Hubert Schölnast Apr 18 '12 at 18:03
  • The 17 is just an arbitrary number. You can insert any number you wish or need. My point is that by explicitly setting the font size, you can get the width of the textLabel as you need. – waylonion Apr 18 '12 at 18:11
  • The point is, that I do NOT want to *SET* (write) the font's size. I want to *GET* (read) it!!! – Hubert Schölnast Apr 18 '12 at 18:17
  • Based on your question, you asked for the width of the textLabel. Thus my answer was focused on obtaining the width of the text label, not the font size. By the way, in this line: textWidth = [cell.textLabel.text sizeWithFont:cell.textLabel.font].width; don't you already provide the font and thus the font size? You already have the font information with: cell.textLabel.font – waylonion Apr 18 '12 at 18:35
  • In `textWidth = [cell.textLabel.text sizeWithFont:cell.textLabel.font].width;` I do not provide a **fix** font size. I provide the size of a font of another text-object. And the core-problem is, that `cell.textLabel.font` always returns a wrong font-size of 0.000. – Hubert Schölnast Apr 18 '12 at 18:43