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:
Text View:
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
Shift Elements in Array by Index
iOS Keeping Old Launch Screen and App Icon After Update
Crashlytics: "We'Re Missing a Dsym to Process Crashes"
Foreign Key Relationship Mapping with Restkit
Custom Mkannotation Callout Bubble with Button
What Is "Self" Used for in Swift
Programmatically Creating an Expanding Uitableviewcell
How to Set the Height of Tableheaderview (Uitableview) with Autolayout
Core Animation Progress Callback
Is It Considered a Private API to Use App-Prefs:Root
Secitemadd and Secitemcopymatching Returns Error Code -34018 (Errsecmissingentitlement)
Ios: Keep an App Running Like a Service
Iad Is Shutting Down. Should I Remove the Iad Framework from All My Applications
Xcode Stuck at "Your Application Is Being Uploaded"
How to Release a Cgimageref in iOS
App Rejected Because of "Missing Push Notification Entitlement"