I have a UITextView with disabled scrolling and I need to set the height of the UITextView to match the height of the text content. I have tried various methods found the internet with no results. This is my code for the layout of the UITextView:
-(void) viewDidLayoutSubviews {
// UITextView is named jobDescrip
jobDescrip.scrollEnabled = NO;
[jobDescrip sizeToFit];
[jobDescrip layoutIfNeeded];
}
I have also tried this code in the viewDidAppear: method. Can someone please post code for an iOS 7.0 and later solution to this problem using auto layout?
Have you tried this? I added a button, with the flowing code in its action method I can see the height is changed to the text height.
Edit
I added a bottom space to superview constraint and set it as IBOutlet property,then in viewDidAppear I changed the constraint constant.
Try adding this line to your method:
and see if it works, if not, try using a
UILabel
instead of aUITextView
, setting itsnumberOfLines
to0
, and adding this method:These are the couple of libraries handling resizing UITextView while user enters text.
CSGrowingTextView: This is a UITextView subclass which resizes self according to its content. This works well for both autolayout and non-autolayout. This provides some more control over resize animation via following properties and delegate methods.
ResizableTextView: This is again a UITextView subclass which encapsulate all size-changing related code.This is based on autolayout. This searches for height constraint added to textView and updates it with calculated content size. Here is the sample usage.
MBAutoGrowingTextView: This is one of the elegant solutions based in auto-layout. This UITextView subclass automatically grows and shrinks based on content and can be constrained by maximal and minimal height - all without a single line of code.
GrowingTextViewHandler:This an NSObject subclass which encapsulated the code for resizing UITextView. This approach is based on autolayout. Handling resize out of UITextView subclass gives much more flexibility. First, GrowingTextViewHandler does not have to worry about handling UITextViewDelegates. Second it will work for any UITextView subclass.