iOS:UITextView设置高度等于内容高度

我有一个禁用滚动的UITextView,我需要设置UITextView的高度以匹配文本内容的高度。 我已经尝试了各种方法find了互联网没有结果。 这是我的代码UITextView的布局:

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

我也在viewDidAppear:方法中试过这段代码。 有人可以请张贴代码为iOS 7.0和更高版本的解决scheme使用自动布局这个问题?

你尝试过吗? 我添加了一个button,stream动代码在它的动作方法中,我可以看到高度改变为文本高度。

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

编辑

我添加了超级查看约束底部空间,并将其设置为IBOutlet属性,然后在viewDidAppear我改变了约束常量。

 - (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]; } 

这些是在用户input文本时处理调整UITextView大小的库。

  1. CSGrowingTextView :这是一个UITextView子类,根据其内容调整自己的大小。 这适用于自动布局和非自动布局。 这通过以下属性和委托方法提供了对调整animation大小的更多控制。

     @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; 
  2. ResizableTextView :这又是一个UITextView子类,封装了所有改变大小的相关代码。这是基于自动布局的。 这将search添加到textView的高度约束,并使用计算的内容大小更新它。 以下是示例用法。

     [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] 
  3. MBAutoGrowingTextView :这是基于自动布局的优雅解决scheme之一。 这个UITextView子类根据内容自动增长和缩小,并且可以被最大和最小高度约束 – 所有这些都没有一行代码。

     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. 
  4. GrowingTextViewHandler :这是一个封装了用于调整UITextView大小的代码的NSObject子类。 这种方法基于自动布局。 处理resize的UITextView子类提供了更多的灵活性。 首先,GrowingTextViewHandler不必担心处理UITextViewDelegates。 其次它将适用于任何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 

尝试添加此行到您的方法:

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

并看看它是否工作,如果没有,尝试使用UILabel而不是UITextView ,将其numberOfLines设置为0 ,并添加此方法:

 - (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; }