IOS: UITextView set height equal to content height

I have a UITextView with scrolling disabled, and I need to set the height of the UITextView to match the height of the text content. I tried various methods that found the Internet without any results. This is my code for building a UITextView:

-(void) viewDidLayoutSubviews { // UITextView is named jobDescrip jobDescrip.scrollEnabled = NO; [jobDescrip sizeToFit]; [jobDescrip layoutIfNeeded]; } 

I also tried this code in the viewDidAppear: method. Can someone please send code to solve iOS 7.0 and later versions of this problem using automatic linking?

0
ios xcode uitextview sizetofit
Nov 04 '14 at 16:39
source share
3 answers

Have you tried this? I added a button with the current code in my action method, I see that the height changes to the height of the text.

 CGSize size = [self.textView systemLayoutSizeFittingSize:self.textView.contentSize]; CGRect frame = self.textView.frame; frame.size.height = size.height; self.textView.frame = frame; 

Edit

I added a lower space to restrict the supervisor and set it as an IBOutlet property, then in viewDidAppear I changed the restriction constant.

 - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; self.textView.contentInset = UIEdgeInsetsZero; CGSize size = [self.textView systemLayoutSizeFittingSize:self.textView.contentSize]; self.heightConstraint.constant -= size.height - self.textView.frame.size.height; self.textView.scrollEnabled = NO; [self.textView setNeedsUpdateConstraints]; } 
+1
Nov 05 '14 at 4:42
source share

These are two libraries that handle resizing a UITextView when a user enters text.

  • CSGrowingTextView : This is a subclass of UITextView that resizes according to its content. This works well for both autorun and non-automation. This provides some control over resizing animations using the following delegation properties and methods.

     @property (nonatomic, readwrite) CSGrowDirection growDirection; @property (nonatomic, readwrite) NSTimeInterval growAnimationDuration; @property (nonatomic, readwrite) UIViewAnimationOptions; - (void)growingTextView:(CSGrowingTextView *)growingTextView willChangeHeight:(CGFloat)height; - (void)growingTextView:(CSGrowingTextView *)growingTextView didChangeHeight:(CGFloat)height; 
  • ResizableTextView : This is again a subclass of UITextView that encapsulates all resizing related codes. It is based on autorun. This searches for the height limit added to the textView and updates it with the calculated content size. Here is a usage example.

     [self.view layoutIfNeeded]; // do your own text change here. self.infoTextView.text = [NSString stringWithFormat:@"%@, %@", self.infoTextView.text, self.infoTextView.text]; [self.infoTextView setNeedsUpdateConstraints]; [self.infoTextView updateConstraintsIfNeeded]; [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{ [self.view layoutIfNeeded]; } completion:nil] 
  • MBAutoGrowingTextView : This is one of the elegant solutions based on automatic layout. This subclass of UITextView automatically grows and shrinks based on the content and can be limited by the maximum and minimum height - all without a single line of code.

     1. Give appropriate horizontal constraint 2. If you want textView to grow upwards, pin to bottom. Otherwise pin it to the top. 3. To limit maximal height, add the height constant with relation "Less than or equal" to some maximum value. 4. To limit minimal height, add the height constant with relation "Greater than or equal" to some minimum value. 
  • GrowingTextViewHandler : This is an NSObject subclass that encapsulated the code for resizing a UITextView. This approach is based on autorun. Resizing handling from a subclass of UITextView provides much more flexibility. First, GrowingTextViewHandler does not have to worry about working with UITextViewDelegates. Secondly, it will work for any subclass of UITextView.

     /*You need to pin UITextView appropriately. 1. Give appropriate horizontal constraint 2. Give height constraint 3. To grow upwards, give bottom constraint To grow downwards, give top constraint To grow both directions, pin it vertically center */ @interface ViewController ()<UITextViewDelegate> @property (weak, nonatomic) IBOutlet UITextView *textView; @property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint; @property (strong, nonatomic) GrowingTextViewHandler *handler; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.handler = [[GrowingTextViewHandler alloc]initWithTextView:self.textView withHeightConstraint:self.heightConstraint]; [self.handler updateMinimumNumberOfLines:3 andMaximumNumberOfLine:8]; } - (void)textViewDidChange:(UITextView *)textView { [self.handler resizeTextViewWithAnimation:YES]; } @end 
+1
Jun 03 '15 at 11:42 on
source share

Try adding this line to your method:

 [jobDescrip.textContainer setSize:jobDescrip.frame.size]; 

and see if it works, if not, try using a UILabel instead of a UITextView , setting its numberOfLines to 0 and adding this method:

 - (float)getHeightFortheDynamicLabel:(NSString *)stringForTheLabel { CGSize maxSize = CGSizeMake(215, 2000.0); CGSize newSize = [stringForTheLabel sizeWithFont:[UIFont systemFontOfSize:14.0] constrainedToSize:maxSize]; //Remember to ensure the font size is both the same in here, and in the label properties. return newSize.height; } 
0
Nov 04
source share



All Articles