Dynamic UITextView height in UITableView-the final solution?

Discussion in 'iOS Programming' started by billo978, Sep 3, 2015.

  1. billo978 macrumors newbie

    billo978

    Joined:
    Sep 3, 2015
    #1
    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 :).
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    If the content is HTML, why not use UIWebView?
     
  3. billo978 thread starter macrumors newbie

    billo978

    Joined:
    Sep 3, 2015
    #3
    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.
     
  4. billo978 thread starter macrumors newbie

    billo978

    Joined:
    Sep 3, 2015
    #4
    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?
     
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
  6. billo978, Sep 3, 2015
    Last edited: Sep 3, 2015

    billo978 thread starter macrumors newbie

    billo978

    Joined:
    Sep 3, 2015
    #6
    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.
     
  7. billo978 thread starter macrumors newbie

    billo978

    Joined:
    Sep 3, 2015
    #7
    Did you try by yourself and did it work for you?

    Thanks.
     
  8. PhoneyDeveloper, Sep 9, 2015
    Last edited: Sep 9, 2015

    PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    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.
     
  9. vvrinne, Sep 28, 2015
    Last edited by a moderator: Sep 28, 2015

    vvrinne macrumors member

    Joined:
    Feb 28, 2010
    Location:
    Helsinki, Finland
    #9
    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.
     
  10. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #10
    @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.
     
  11. billo978 thread starter macrumors newbie

    billo978

    Joined:
    Sep 3, 2015
    #11
    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.
     

Share This Page