views:

137

answers:

3

Hello,

I am using the method [string sizeWithFont:constrainedToSize:lineBreakMode:] to estimate the height of a textView that I am resizing. However, it seems to consistently return the incorrect size. To debug, I wrote the following code:

self.textView.font = [UIFont systemFontOfSize:14.0]; // It was this before, anyways
NSLog(@"Real width: %lf %lf", self.textView.contentSize.width, self.textView.frame.size.width);
NSLog(@"Real height: %lf", self.textView.contentSize.height);
NSLog(@"Estimated width: %lf", kOTMessageCellTextWidth);
NSLog(@"Estimated height: %lf", ([message.message sizeWithFont:[UIFont systemFontOfSize:14.0]
                                             constrainedToSize:CGSizeMake(kOTMessageCellTextWidth, CGFLOAT_MAX)
                                                 lineBreakMode:UILineBreakModeWordWrap].height));

However, the above code reveals that I am getting inconsistent results:

Real width: 223.000000 223.000000
Real height: 52.000000
Estimated width: 223.000000
Estimated height: 36.000000
Real width: 223.000000 223.000000
Real height: 142.000000
Estimated width: 223.000000
Estimated height: 126.000000
Real width: 223.000000 223.000000
Real height: 142.000000
Estimated width: 223.000000
Estimated height: 126.000000

I noticed in this similar question that (apparently) textView has some padding that constrains its actual width. The recommendation there was the decrease the width passed to sizeWithFont: by some number. The recommended number was 11.

My question: is there any way to actually retrieve this value programmatically, or is there some documentation that I missed specifying this number? It seems like this number should be available and shouldn't have to be guess-and-checked, but I can't find a reliable way to identify it.

Thanks in advance.

A: 

You could try checking the contentInset value (a property of UIScrollView).

nevan
Looks like `contentInset` is returning `0.0f`...
Ashoat
A: 

I use following function without much problem:

+(float) calculateHeightOfTextFromWidth:(NSString*) text: (UIFont*)withFont: (float)width :(UILineBreakMode)lineBreakMode

{
    [text retain];
    [withFont retain];
    CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];

[text release];
[withFont release];

return suggestedSize.height;
}
neha
That is certainly not working for me. Are you dealing with a `UITextView`, or something else?
Ashoat
I apply this to label but I'm sure this will work in case of textview. I am calling this class method as: float textHeight = [class_name_where_this_method_resides calculateHeightOfTextFromWidth:_content : content.font :content.frame.size.width :UILineBreakModeWordWrap]; where _content is my actual text and content is my label's name.
neha
A: 

I've also struggled with the problem. I've usually gotten by by passing the sizeWithFont method a size that's smaller than the actual size of the textView. Even when adding the textview's content insets to the size, it doesn't work. The size returned is always smaller.

  • charlie
CharlieMezak