Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>try using: </p> <pre><code>[textView.text boundingRectWithSize:CGSizeMake(txtFrame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:textView.font,NSFontAttributeName, nil] context:nil]; </code></pre> <p>The string measuring seems pretty buggy. This is the only option combination that gives the right size, for the testing I have done.</p> <p>I'm using the following code with success in iOS7 (it's a <code>UITextField</code> with a minimum and maximum height. When the text's height get larger then <code>MAX_HEIGHT_MESSAGE_TEXTBOX</code>, scrollbars appears in the <code>UITextField</code>).</p> <pre><code>const float MAX_HEIGHT_MESSAGE_TEXTBOX = 80; const float MIN_HEIGHT_MESSAGE_TEXTBOX = 30; - (void)setFrameToTextSize:(CGRect)txtFrame textView:(UITextView *)textView { if(txtFrame.size.height &gt; MAX_HEIGHT_MESSAGE_TEXTBOX) { //OK, the new frame is to large. Let's use scroll txtFrame.size.height = MAX_HEIGHT_MESSAGE_TEXTBOX; textView.scrollEnabled = YES; [textView scrollRangeToVisible:NSMakeRange([textView.text length], 0)]; } else { if (textView.frame.size.height &lt; MIN_HEIGHT_MESSAGE_TEXTBOX) { //OK, the new frame is to small. Let's set minimum size txtFrame.size.height = MIN_HEIGHT_MESSAGE_TEXTBOX; } //no need for scroll textView.scrollEnabled = NO; } //set the frame textView.frame = txtFrame; } - (void)setframeToTextSize:(UITextView *)textView animated:(BOOL)animated { //get current height CGRect txtFrame = textView.frame; //calculate height needed with selected font. Note the options. //append a new line to make space for the cursor after user hit the return key txtFrame.size.height =[[NSString stringWithFormat:@"%@\n ",textView.text] boundingRectWithSize:CGSizeMake(txtFrame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:textView.font,NSFontAttributeName, nil] context:nil].size.height; if (animated) { //set the new frame, animated for a more nice transition [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseOut |UIViewAnimationOptionAllowAnimatedContent animations:^{ [self setFrameToTextSize:txtFrame textView:textView]; } completion:nil]; } else { [self setFrameToTextSize:txtFrame textView:textView]; } } - (void)textViewDidChange:(UITextView *)textView { [self setframeToTextSize:textView animated:YES]; } </code></pre> <p><strong>EDIT</strong></p> <p>When the string measuring is correct, you might need to change the <code>lineBreakMode</code>on the <code>UITextView</code>'s textContainer. (<code>NSTextContainer</code> is a new class in iOS7, containing information about how text should be laid out):</p> <pre><code>textView.textContainer.lineBreakMode = NSLineBreakByCharWrapping; // default is NSLineBreakByWordWrapping </code></pre> <p>Good luck!</p>
 

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