Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble when trying to change UITextView height dynamically
    primarykey
    data
    text
    <p>I'm using iOS7.</p> <p>I want to change the height of my UITextView dynamically according to the content height. However, I find in iOS7, the contentSize is a mess. For example, when I delete a line, the contentSize will not change, but I expect its height to decrease.</p> <p>So I write the following </p> <pre><code>- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { // increase a line if ([text isEqualToString:@"\n"]) { _FRAME_SET_H(textView, textView.contentSize.height + 24.0f); // decrease a line } else if (range.length == 1 &amp;&amp; [textView.text hasSuffix:@"\n"]) { _FRAME_SET_H(textView, textView.contentSize.height - 24.0f); } else { _FRAME_SET_H(textView, textView.contentSize.height); } return YES; } </code></pre> <p>Everything works fine, until I input a very long text:</p> <p><img src="https://i.stack.imgur.com/5TXad.png" alt="enter image description here"></p> <p>The contentSize will not change when the text is long enough to cause a automatically line change!</p> <p>Could anyone help me?</p> <p>What I want is simple: change the height of the UITextView according to its content. However, I have spent more than two days on it, but still not solved. </p> <p>The UITextView in iOS7 is so bug.</p> <hr> <p>I tried to follow Uptown Apps's solution. Following is my new code (changed a bit to fit in my other project):</p> <pre><code>- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { return YES; } - (void)textViewDidChange:(UITextView *)textView { CGRect rect = [textView.text boundingRectWithSize:CGSizeMake(300, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: textView.font} context:nil]; _FRAME_SET_H(textView, rect.size.height); _FRAME_SET_H(self, _FRAME_H(textView) + _BLOCK_PADDING * 2); } </code></pre> <p>However, trouble remains. In the following image, the white rect is the UITextView, and the black background is its superview.</p> <p>When I start the app, the UITextView's height is 0, and the screen looks like: <img src="https://i.stack.imgur.com/yGk79.png" alt="enter image description here"></p> <p>Then I type 'A', the screen turns into: <img src="https://i.stack.imgur.com/Xrc5g.png" alt="enter image description here"></p> <p>You see there is two problems: 1) the height is not enough. 2) the caret is strange!</p> <p>Then I type some more 'a', the screen turns into: <img src="https://i.stack.imgur.com/f443b.png" alt="enter image description here"></p> <p>Then I type a 'return' to change a line, the screen turns into: <img src="https://i.stack.imgur.com/JO6eL.png" alt="enter image description here"></p> <p>Yes, it does not change! But I expect it to increase its height to include the new blank line.</p> <p>Then I type a 'A', the screen turns into: <img src="https://i.stack.imgur.com/1Ps5P.png" alt="enter image description here"></p> <p>The height is increased, but the new text is not showing, neither is the caret. I guess it is also due to that the height is not enough.</p> <p>Then I type another 'a', the screen turns into: <img src="https://i.stack.imgur.com/AnChU.png" alt="enter image description here"></p> <p>Letters appear, but only a half...</p> <p>By the way, when I use UILabel, I can get the correct text height using this method!!!! (i.e. calling boundingRectWithSize:)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload