How does I size a uitextview to its content?

Source: Internet
Author: User

Uitextview adaptive height, moved to a stack on: Is there a good the will to adjust the size of a UITextViewTo conform to its content? Say for instance I has a UITextViewthat contains one line of text:

"Hello World"

I then add another line of text:

"Goodbye World"

Is there a good on Cocoa Touch to get the rect, that'll hold all of the lines in the text view so that I can adjust T He parent view accordingly?

As another example, look at the Notes field for events in the Calendar application--note how the cell (and the UITextView it co Ntains) expands to hold any lines of the text in the notes string.

In my (limited) experience,

- (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode

Does not respect newline characters, so can end up with a lot shorter CGSize than are actually required.

- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size

Does seem to respect the newlines.

Also, the text isn ' t actually rendered at the top of the UITextView . In my code, I set the new height of the UITextView pixels larger than the height returned by the sizeOfFont methods.

This works for both iOS 6.1 and iOS 7:

- (void)textViewDidChange:(UITextView *)textView{    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;}

Or in Swift

    let fixedWidth = textView.frame.size.width    textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))    let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))    var newFrame = textView.frame    newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)    textView.frame = newFrame;

If you want support for IOS 6.1 and then you should also:

textview.scrollEnabled = NO;

There is actually a very easy-to-do resizing of the UITextView content. The It can is done using the UITextView contentSize .

CGRect frame = _textView.frame;frame.size.height = _textView.contentSize.height;_textView.frame = frame;

One thing to note are that the correct are only available after the have contentSize been added to the view with UITextView . Prior to that it's equal toframe.size

This won't work if auto layout is on. With auto layout, the general approach are to use the sizeThatFits method and update the constant value on a height constraint.

CGSize sizeThatShouldFitTheContent = [_textView sizeThatFits:_textView.frame.size];heightConstraint.constant = sizeThatShouldFitTheContent.height;

heightConstraintis a layout constraint this typically setup via a iboutlet by linking the property to the height of constraint created in A storyboard.

Just to add-amazing answer, if you:

[self.textView sizeToFit];

There is a difference in behaviour with the iphone6+ only:

With the 6+ only (not the 5s or 6) It does add ' one more blank line ' to the Uitextview. The "RL solution" fixes this perfectly:

CGRect _f = self.mainPostText.frame;_f.size.height = self.mainPostText.contentSize.height;self.mainPostText.frame = _f;

IT fixes the ' extra line ' problem on 6+.

In my (limited) experience,

- (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode

Does not respect newline characters, so can end up with a lot shorter CGSize than are actually required.

- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size

Does seem to respect the newlines.

Also, the text isn ' t actually rendered at the top of the UITextView . In my code, I set the new height of the UITextView pixels larger than the height returned by the sizeOfFont methods.

In IOS6, you can check the property of Uitextview right after you contentSize set the text. In IOS7, this would no longer work. If you want to restore this behavior for iOS7, place the following code in a subclass of Uitextview.

- (void)setText:(NSString *)text{    [super setText:text];    if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {        CGRect rect = [self.textContainer.layoutManager usedRectForTextContainer:self.textContainer];        UIEdgeInsets inset = self.textContainerInset;        self.contentSize = UIEdgeInsetsInsetRect(rect, inset).size;    }}

To make a dynamically sizing uitextview inside a UITableViewCell, I found the following combination works in Xcode 6 with The IOS 8 SDK:

    • In Storyboard/ib, add a uitextview to a uitableviewcell and constrain it to the sides
    • In Storyboard/ib, uncheck scrolling enabled (with scrolling enabled, the frame of the Uitextview is independent of the con Tent size, but with scrolling disabled, there is a relationship between the
    • In Viewdidload, tell the TableView to automatically calculate row heights:

      tableView.estimatedRowHeight = 150;tableView.rowHeight = UITableViewAutomaticDimension;

For read-only dynamically sizing uitextviews, that's it. If you ' re allowing users to edit the text in your Uitextview, you also need to:

    • Implement the Textviewdidchange:method of the Uitextviewdelegate protocol, and tell the TableView to repaint itself every Time the text is edited:

      - (void)textViewDidChange:(UITextView *)textView;{    [tableView beginUpdates];    [tableView endUpdates];}
    • And don ' t forget to set the Uitextview delegate somewhere, either in Storyboard/ib or in Tableview:cellforrowatindexpath:

How does I size a uitextview to its content?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.