Reputation: 2085
I have a UITextView called profileDescription that lives inside of a scroll view. I'd like to adjust the height of this view so that it expands to take up as much space as it needs. I'm using Storyboards with Autolayout turned on. I tried turning it off but it made other things worse so I am leaving it on for now.
I am trying to use the following code but it has no effect:
self.profileDescription.text = @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
[self.profileDescription setScrollEnabled:NO];
CGRect frame = self.profileDescription.frame;
frame.size.height = self.profileDescription.contentSize.height;
self.profileDescription.frame = frame;
Upvotes: 0
Views: 532
Reputation: 31339
There is an easy fix, when the Autolayout
feature is turned on.
Implement viewDidLayoutSubviews
, which will call after the view controller's view's layoutSubviews
method is invoked.
Example code:
- (void)viewDidLayoutSubviews
{
self.yourTextField.frame = CGRectMake(361, 226, 400, 100);
}
Upvotes: 3
Reputation: 1699
From what I understand, you want your UITextView to expand to the size of the text, so that its vertical size is as tall as the text.
You need to calculate the height of your text and set the vertical size accordingly (note: I adapted this from a project with a UILabel instead of a UITextView, and I haven't tested this version):
CGSize maxLabelSize = CGSizeMake(somewidth, 9999);
CGSize expectedSize = [yourText sizeWithFont:textView.font constrainedToSize:maxLabelSize lineBreakMode:NSLineBreakByWordWrapping];
CGRect newFrame = textView.frame;
newFrame.size.height = expectedSize.height;
textView.frame = newFrame;
Upvotes: 0
Reputation: 1415
Try explicitly setting the frame of the UITextView before you set the text and see if that makes any difference. UITextView needs to know the width of the text view so it can properly determine what the contentSize.height should be.
Upvotes: 0