PDA

View Full Version : Multiline Message Cut Off by Table Cell...




ArtOfWarfare
Nov 16, 2009, 08:33 PM
I tried asking for help from someone in person, but they weren't willing to stand up and come and look, so instead I had to print out the paper explaining it below. Rather than take the screen shots again and upload them, I figured I'd just upload the PDF of what I printed.

On the left I have two very similar screen shots... the top shows the program running with font size 20, while the bottom shows the program running with font size 14 (which is what it's set to in the example code.) Notice that the middle cell on the top one cuts off "game!!" while the middle cell on the bottom one properly includes it.

On the right, I have what I believe is all the relevant snippets of code

Basically my issue is that neither the text view nor the cell is setting the height properly. Therefor, there seems to be either:
a.) an issue with how I'm using the sizeWithFont:constrainedToSize:lineBreakMode method
b.) an issue with how the above method calculates height.

I suspect the issue lies in the fact that sizeWithFont:constrainedToSize:lineBreakMode is calculating it's height using something other than a UITextView. What makes me think this? Because it includes "lineBreakMode" as one of its parameters. Text wrapping can't be set on a UITextView... but it can be set on a UILabel. The dumb part is that UILabel will ignore anything related to wrapping and instead the only values that make a difference are whether (and where) to truncate, or to simply crop.



KoolStar
Nov 17, 2009, 12:00 AM
Why not just resize the sext to fill size. The tableview has a property that allows that.

ArtOfWarfare
Nov 17, 2009, 04:34 AM
Why not just resize the sext to fill size. The tableview has a property that allows that.

What property of UITableView allows you to set the text to fill size?

I believe you're referring to a property of UILabel (namely, a UILabel within a UITableViewCell)... however the UILabel cannot have multiple lines of text, so instead I have to use the UITextView. The UITextView however, doesn't have any similar property.

Either I'm an idiot or someone at Apple mixed things up when they decided to give the line wrapping to a class that couldn't use it and neglected giving it to the one that could.

(If someone could just write the one line of code I would need to fix this... I've been researching for hours now within Apple docs and various forms, I'm getting very frustrated now...)

Troglodyte
Nov 17, 2009, 08:11 AM
(namely, a UILabel within a UITableViewCell)... however the UILabel cannot have multiple lines of text,
Why does UILabel have this property then?


numberOfLines

The maximum number of lines to use for rendering text.

@property(nonatomic) NSInteger numberOfLines

Discussion

This property controls the maximum number of lines to use in order to fit the label’s text into its bounding rectangle. The default value for this property is 1. To remove any maximum limit, and use as many lines as needed, set the value of this property to 0.

If you constrain your text using this property, any text that does not fit within the maximum number of lines and inside the bounding rectangle of the label is truncated using the appropriate line break mode.

When the receiver is resized using the sizeToFit method, resizing takes into account the value stored in this property. For example, if this property is set to 3, the sizeToFit method resizes the receiver so that it is big enough to display three lines of text.

PhoneyDeveloper
Nov 17, 2009, 08:37 AM
I looked at your source code. One obvious mistake is that you are adding the textview to the cell every time that cellForRowAtIndexPath is called. You should only add the text view one time, when the cell is created, not when it's dequeued.

Here's how I've always debugged this problem: print out the height returned from heightForRowAtIndexPath. The way you have the code written with everything in one expression makes it difficult to debug. Spread out that expression into multiple expressions so you can see all the intermediate results. I think you need to make the textview have an autoResizingMode of flexible width and flexible height. Also, for debugging purposes you can add the willDisplayCell callback and inspect the cell's properties when it's about to draw. Also, the width of the textview isn't the same as the width you're passing to sizeWithFont in heightForRowAtIndexPath, which is one of the usual reasons this doesn't work.

ArtOfWarfare
Nov 17, 2009, 08:42 PM
Why does UILabel have this property then?

Wow, thanks for pointing that out.
I made the mistake of assuming UILabel was always limited to a single line. By getting rid of the whole chunk of code making a UITextView and just using the UILabel that comes with the cell, my code is a lot simpler and now it seems to actually work.

I looked at your source code. One obvious mistake is that you are adding the textview to the cell every time that cellForRowAtIndexPath is called. You should only add the text view one time, when the cell is created, not when it's dequeued.

This is a very good point and I'll keep it in mind in the future when dealing with custom subviews in a UITableViewCell.