I'm trying to make a photo feed for an app I'm making, similar to Instagram's:
I've created a preliminary version using a UITableView for the feed, but it's becoming a hassle dealing with the dynamic nature of each cell. The number of likes and comments along with the comment text itself will determine how tall the cell is. I used UITextViews to draw the comment and like text, and approximate the cell height and positioning using the UITextViews' text with sizeWithFont. This solution seems very imprecise and has a lot of downsides.
I was thinking about using a UIWebView for the entire feed as an alternate solution. It would make positioning the like and comment text extremely simple, along with the ability to have variable font in the text as seen in Instagram. I haven't really used UIWebViews extensively, so I'm not sure how easy or hard it would be to create the whole feed this way.
Should I continue using my UITableView solution or look into redoing it all using a UIWebView?
In my app, I've got a UITableViewCell that dynamically adjusts its height based on the amount of text in the cell. I think it might help answer your question.
Here's the trick: There is no need to "approximate text sizes" --- check out the documentation for the method [NSString sizeWithFont: constrainedToSize: lineBreakMode:]
maximumSize = CGSizeMake(self.contentView.bounds.size.width - TEXT_LEFT_MARGIN * 2.0,
MAXIMUM_TEXT_HEIGHT);
textStringSize = [textLabel.text sizeWithFont:textLabel.font
constrainedToSize:maximumSize
lineBreakMode:textLabel.lineBreakMode];
So let's take a simple example where you want 10 pixels of whitespace above the label, and 15 pixels of whitespace below the label. You'd then generate a CGRect
with the measurements above, with a height of 25 pixels added to the textStringSize.height
value generated above.
Using this method means that your UITableViewCells will scale up and down nicely, regardless of the size of the comments in the different labels.