Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

billo978

macrumors newbie
Original poster
Sep 3, 2015
6
0
Hi guys,

I searched a lot about this topic but didn't find a suitable answer, only pieces of non-working code or oh-my-god non-working "solutions" (i.e. using textViewDidChange, beginUpdates, endUpdates methods) or similar cases (i.e. using a UILabel - I know that it works but I need an UITextView instead; or using sizeWithFont - I can't do that because the content is an HTML).

The problem is simple and I don't know why the answer seems so complicated.

I'm working on iOS 7. I built a XIB (linked to a UITableViewCell subclass) that contains a UITextView. These textviews in the table are populated with texts of different lengths. The width must not change, the height should.

The desired behaviour is that the height of the cell increases or decreases depending on the text length.

Do you guys have a working code or could you please link me a suitable tutorial or give me some hint?

Thank you very much :).
 
If the content is HTML, why not use UIWebView?
Hi dejo, thanks for your answer.

I'm not sure if I can use UIWebViews, because in the rest of the project I'm working on only DTAttributedTextView object are used.
 
If the content is HTML, why not use UIWebView?
Out of curiosity I just tried to replace DTAttributedTextView with UIWebView in the XIB but the height of the cell doesn't change.

How could I fill a UIWebView with some HTML content and then ask it that its size should increase/decrease depending on the actual content?
 
You probably have to set the textviews to not scroll and make sure you have constraints for all four sides of the textview to the contentView.

Try this.
https://www.captechconsulting.com/blogs/ios-8-tutorial-series-auto-sizing-table-cells
Hi PhoneyDeveloper, thanks for your answer.

Already tried that. My UITextView has 4 constraints to the contentView (top, bottom, trailing, leading) and scrolling is disabled.

In the page you linked there is another link to John Szumski's tutorial about dynamic cells on iOS7. But again it's based on UILabels, not on UITextViews.
 
Last edited:
Yes it works for me. Besides the 4 constraints you need this code in viewDidLoad:

Code:
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 44; // or some other value

Hmmm, I notice you're working on iOS 7. I was doing this on iOS 8. When I tried it in the iOS 7 Sim it doesn't work. There seems to be a significant amount of complaints in the forums about problems with self-sizing table view cells on iOS 7. I wasn't able to find a resolution to the problem.
 
Last edited:
  • Like
Reactions: dejo
What I ended up doing in a recent app was calculating the textview height based on the amount of text and then adding this value to the cell height returned from heightForRowAt. So basically I am calling something like this for each item in my table:

Code:
func getCommentHeight(item: NewsCommentItem) -> CGFloat {
  txtView.text = item.content
  var rect = txtView.sizeThatFits(CGSizeMake(tableView.bounds.width - 10 , CGFloat.max))
  return COMMENT_CELL_HEIGHT + (rect.height - 72)
}

And obviously you need to initialize the textview with the correct font:

var txtView = UITextView()
txtView.font = UIFont(name: "HelveticaNeue", size: 16)

So these values are stored in an array when the data is updated and then used for calculating the correct row height when heightForRowAt is called. Not sure what the "proper" way to achieve this would be, but this is at least one method that works.
 
Last edited by a moderator:
@vvrinne, Right. That's the old way. The new way that works in iOS 8 and later is simply to set up vertical constraints like I mention in #5 and #8 above. heightForRowAtIndexPath is no longer needed.
 
Hi vvrinne, thanks for your reply.

Unfortunately I don't have a fixed font size in the textview, because - as I wrote before - the content is HTML and the font size may vary depending on the tags.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.