14

When I test my app on IOS7 simulator. Sometimes I found it is weird when I using sizeToFit of a UITextView. The frame after sizeToFit seems right but the text can only show partly just like the photo below. (The gray area represents the UITextView new frame after sizeToFit, the whole sentence should be "which sparked a tense relationship between the two.")

enter image description here

The UITextView text is set via attributedText. It seems the problem occurs with some sentences only and is OK for most sentences.

I met this problem several times and can not solve it yet. Any help will be appreciated. Thanks.

Update:

Finally I solve the problem in an ugly way. I reset the text of the textView.

NSString *text = textView.text;
textView.text = @"";
textView.text = text;

Now it can show the whole content after sizeToFit. I think it seems like a IOS 7's bug.

echo
  • 1,244
  • 1
  • 16
  • 40
  • 1
    Increase the Textview frame height. – Romance Sep 21 '13 at 05:06
  • I already set the backGroundColor of the UITextView to gray color. (the gray area of the photo above). The Textview frame height is enough to show the whole text. – echo Sep 21 '13 at 06:03
  • Maybe [textView setNeedDisplay] ? – Kyle Fang Sep 22 '13 at 10:45
  • To Kyle Fang, Thanks for reply. I tried setNeedDisplay and it did not work. – echo Sep 23 '13 at 00:53
  • To CupraR_On_Rails: Finally I solve the problem in an ugly way. I reset the text of the textView. NSString *text = textView.text; textView.text = @""; textView.text = text; I have updated my original post. – echo Oct 11 '13 at 09:28

4 Answers4

18

I had the same problem, it took me a while to figure it out, you simply need to resize the text container

[textview sizeToFit];
[textview.textContainer setSize:textview.frame.size];
Ahmed Hossny
  • 272
  • 4
  • 12
3

Noticed in IOS7 sizeToFit wasn't working also - perhaps the solution may help you too, needs the additional layoutIfNeeded

[UITEXTVIEW sizeToFit];
[UITEXTVIEW layoutIfNeeded];
[UITEXTVIEW setTextContainerInset:UIEdgeInsetsMake(0, 0, 0, 0)];
Nick Wood
  • 680
  • 6
  • 10
  • This was the difference of going onto new lines and not - SAVED! - thank you and thanks Apple for default padding/margins that took days to work out :) – Henry Heleine Nov 19 '20 at 21:39
0

Have you got "clip subviews" checked in interface builder?

Try to uncheck it or setting this property to "NO".

Xavier Maroñas
  • 3,124
  • 1
  • 14
  • 7
  • I create UITextView by coding and not used interface builder. I can not find the clipSubviews property also. Can you give me some hints? – echo Sep 25 '13 at 08:30
  • this can be done in code by setting `clipsToBounds = NO`, but this doesn't appear to solve the problem. – alexshafran Oct 11 '13 at 17:37
0

Try this it worked for me.

[textView setTextContainerInset:UIEdgeInsetsMake(0, 0, 0, 0)];
Karthik_S
  • 119
  • 1
  • 8