2

I have a uitextview, I would like to dynamically load it based on the contents, the answer from this link doesnt work on iOS11. https://stackoverflow.com/a/20999067/365384

  CGFloat fixedWidth = textView.frame.size.width;
  CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
  CGRect newFrame = textView.frame;
  newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
   textView.frame = newFrame;

The only way I can do is use constraint update constraint

CGFloat fixedWidth = self.textView .frame.size.width;
CGSize newSize = [self.textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
    self.textViewHeightConstraint.constant = newSize.height;

I am working on a universal app, and I dont want use too many constraints in the code. Is there any way to fix it without adding extra constraint?

bitma
  • 117
  • 1
  • 15
  • Your question is not clear... What are you trying to ***do***? *Why* do you need to get the height? *When* do you need to get the height? – DonMag Nov 22 '17 at 17:02
  • I want to display a paragraph with hyperlink in the screen, the paragraph is from server so I dont know the actual length of it before I got the response from server. Then I tried different ways to update uitextview height based on contents, and all these ways failed except updating constraint. – bitma Nov 23 '17 at 18:33
  • OK - that still doesn't tell me why you need to get the height. Have you set constraints in storyboard? `UITextView` will auto-adjust its own height if you let it. Set the top, leading and trailing constraints, but don't set bottom or height constraints. Disable scrolling. Set data-detectors as desired. Set Editable and Selectable as desired. Now, when you change the text of the textview, its height will change. No need for sizeToFit or sizeThatFits or changing constraints or forcing layout subviews. – DonMag Nov 24 '17 at 23:18
  • I didnt set contraints, I only set height >= 10, then UITextView didnot auto adjust height based on the content. So I have to call layoutsubviews to force it layout contents based on the content size. – bitma Dec 07 '17 at 21:08
  • I put up an example app to demonstrate: https://github.com/DonMag/TextViewAutoSize – DonMag Dec 07 '17 at 21:31

2 Answers2

1

I had a similar issue and found that I had to set scrollingEnabled to false, then sizeToFit() would work.

Andy Shephard
  • 1,726
  • 17
  • 26
0

I have tried layoutifneeds, setneedforlayout, all these functions does not resize the uitextview's containerview, even you change the containerview's size, the uitextview will remain original size.

After look into the UI hierarchy of uitextview, I figure out a easy way to resize uitextview height is layoutsubviews.

[self.textview sizeToFit];
[self.textview layoutSubviews];

You may need to set textview height >= min height in xib file.

there are a lot questions regarding dynamic height of uitextview, and I cant find this answer, unfortunate I cant post reply to these questions, if you think this answer is helpful, please share it with other similar questions.

Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
bitma
  • 117
  • 1
  • 15
  • Note that Apple docs indicate that layoutSubviews should not be called directly. It states: You should not call this method directly. If you want to force a layout update, call the setNeedsLayout() method instead to do so prior to the next drawing update. If you want to update the layout of your views immediately, call the layoutIfNeeded() method. – KenB May 08 '19 at 00:10