4

I have a UITableViewCell subclass that contains a UITextView where scrolling is turned off. I set its frame like this, in my table view cell layoutSubviews method:

    CGRect frame = self.bodyTextView.frame;
    CGSize size = self.bodyTextView.contentSize;
    frame.size = size;
    self.bodyTextView.frame = frame;

This has been working fine for some time, but I've noticed that in a case where I have a particularly large amount of text, the text is getting cut off. I've set the text view frame background color to orange so I could verify that the frame was being set correctly. Here is an example (I am only showing the bottom portion of the text view):

enter image description here

The frame is the correct size based on the text (in this case 1019 points), but the text stops before the bottom of the text view. I have also seen the text get cut off part way through a line, (ie the text of the last visible line of text is cut off half way through horizontally). Does anyone have an idea what is happening here?

A few other points:

  • The text views work well for all my table view cells with shorter content.
  • If I increase the amount of text in the case shown above, the text view height increases, but the text still gets cut off at the same place.
Darren
  • 10,091
  • 18
  • 65
  • 108

3 Answers3

4

According to this and similar answers, the problem could be that "the correct contentSize is only available after the UITextView has been added to the view ... Prior to that it is equal to frame.size"

I'd suggest you to calculate the height in a different way, like -sizeWithFont: or -sizeTofit

Community
  • 1
  • 1
dimuz
  • 327
  • 2
  • 11
  • Thanks for the link. It's interesting, but my frame already seems to be set correctly, it is just the text that is getting cut off for some reason. I tried calculating the frame height myself, and using sizeToFit, but they had no effect. – Darren May 30 '13 at 13:42
  • I'm really sorry, i misunderstood the problem. So resizing uitextview's frame seem not affecting a new draw of the contentsize in a bigger space. Have u already had a look on this [question?](http://stackoverflow.com/questions/16301147/why-uitextview-draws-text-in-bad-frame-after-resizing) – dimuz May 30 '13 at 14:47
  • That led me to my answer: I forgot to call [super layoutSubviews] when I overode it in my table view cell class...sigh... – Darren May 30 '13 at 14:53
  • In my case, the problem was not the length nor the size, but the content. I was using stripHTML to take out HTML tags on the way into the UITextView: **self.textView.text = [self.sourceText stripHTML];** There was some bad HTML in the source and that caused the incomplete content. – plm Jun 12 '14 at 21:42
1

Use this to get the textSize

// find the text width; so that btn width can be calculate
CGSize textSize = [@"YOUR TEXT" sizeWithFont:[UIFont fontWithName:@"Arial" size:20.0] 
                              constrainedToSize:CGSizeMake(320.0f, 99999.0f)
                                  lineBreakMode:UILineBreakModeWordWrap];

and then set height using this as :

CGRect frame = self.bodyTextView.frame;

frame.size = textSize;
self.bodyTextView.frame = frame;

Hope it helps you.

pkamb
  • 33,281
  • 23
  • 160
  • 191
Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61
  • Thanks for the suggestion. My frame is already getting set correctly though, it is just the text that is getting cut off for some reason. I did try this solution just to see what would happen, but there was no change. – Darren May 30 '13 at 13:40
  • Have you created your textview programatically or through NIB ? – Nishant Tyagi May 30 '13 at 13:44
  • It's in my subclassed UITableViewCell xib. – Darren May 30 '13 at 13:46
  • Can you use it programatically. I assume its a bug in ios. CHECK IT BY CREATING PROGRAMATICALLY WHETHER IT IS WORKING OR NOT. – Nishant Tyagi May 30 '13 at 13:47
  • 1
    The problem was I forgot to call [super layoutSubviews] when I overode it in my table view cell subclass. – Darren May 30 '13 at 14:54
  • @Darren where did you called `[super layoutSubviews]`. I have the same problem where last line of text is getting cut off. I have tried various height calculations and im very confident that height calculation is not wrong. I have subclassed `UITableViewCell` class and my textview is in xib. – Sourabh Bhardwaj Dec 20 '14 at 10:54
  • @sourabh This first line of the layoutSubviews method in my UITableViewCell subclass. – Darren Dec 20 '14 at 17:37
  • thanks @Darren .. the issue still happens in my case. Looking for some other solution. – Sourabh Bhardwaj Dec 21 '14 at 16:38
1

I had the same issue and resolved it working on the textView padding (in swift5):

yourTextView.textContainerInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)

Hope it can help :)