Uitextview That Expands to Text Using Auto Layout

UITextView that expands to text using auto layout

The view containing UITextView will be assigned its size with setBounds by AutoLayout. So, this is what I did. The superview is initially set up all the other constraints as they should be, and in the end I put one special constraint for UITextView's height, and I saved it in an instance variable.

_descriptionHeightConstraint = [NSLayoutConstraint constraintWithItem:_descriptionTextView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.f
constant:100];

[self addConstraint:_descriptionHeightConstraint];

In the setBounds method, I then changed the value of the constant.

-(void) setBounds:(CGRect)bounds
{
[super setBounds:bounds];

_descriptionTextView.frame = bounds;
CGSize descriptionSize = _descriptionTextView.contentSize;

[_descriptionHeightConstraint setConstant:descriptionSize.height];

[self layoutIfNeeded];
}

Get UITextView dynamic height with auto layout after setting text

This should work:

NSLog(@"text before: %.2f",self.myText.frame.size.height);
[self.myText setText:self.string];
[self.myText layoutIfNeeded]; // <--- Add this
NSLog(@"text after: %.2f",self.myText.frame.size.height);

Here's an example implementation on my Github: https://github.com/guillaume-algis/SO-27060338

Auto Layout - Make UITextView, not UIImageView, stretch the container view

You should set the vertical content compression resistance priority of the image view to be lower than the vertical content hugging priority of the text view. For example, I've set the former to "low" and the latter to "high", and it works.

Image view:

Sample Image

Text View:

Sample Image

UITextView Dynamic Height Using Autolayout - Top line of text cutoff with each new line

Oh, sorry for necroposting, but I had the same problem and finally found a solution.

So basicly u neeed:

if textView.contentSize.height <= textView.height {
textView.scrollRangeToVisible(NSMakeRange(0, 0))
}

It will scroll text to correct position if u didn't reached maximum toolbar height. If u reached it, than ur text view content size is higher than its height and u shouldn't have this problem.

Edit: Credits to
https://stackoverflow.com/a/19047464/1316040 for mentioning HPGrowingTextView.
https://github.com/HansPinckaers/GrowingTextView for... well, existing
https://github.com/KennethTsang/GrowingTextView for actual line of code scrollRangeToVisible

How do I size a UITextView to its content?

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 (Works with Swift 4.1 in iOS 11)

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

If you want support for iOS 6.1 then you should also:

textview.scrollEnabled = NO;

UITextView in UIScrollView using auto layout

Update: My answer was bad. Better approach is presented in Scroll Views Inside Scroll Views.



Related Topics



Leave a reply



Submit