简体   繁体   English

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

[英]iOS: UITextView set height equal to content height

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. 我有一个禁用了滚动的UITextView,我需要设置UITextView的高度以匹配文本内容的高度。 I have tried various methods found the internet with no results. 我尝试了各种方法,但没有找到结果。 This is my code for the layout of the UITextView: 这是我的UITextView布局代码:

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

I have also tried this code in the viewDidAppear: method. 我也在viewDidAppear:方法中尝试过此代码。 Can someone please post code for an iOS 7.0 and later solution to this problem using auto layout? 有人可以使用自动布局发布iOS 7.0及更高版本解决此问题的代码吗?

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. 我添加了一个按钮,在其动作方法中添加了流畅的代码,我可以看到高度已更改为文本高度。

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 bottom space to superview constraint and set it as IBOutlet property,then in viewDidAppear I changed the constraint constant. 我为超级视图约束添加了一个底部空间,并将其设置为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];
}

These are the couple of libraries handling resizing UITextView while user enters text. 这些是在用户输入文本时处理UITextView大小调整的两个库。

  1. CSGrowingTextView : This is a UITextView subclass which resizes self according to its content. CSGrowingTextView :这是一个UITextView子类,可根据其内容调整自身大小。 This works well for both autolayout and non-autolayout. 这适用于自动布局和非自动布局。 This provides some more control over resize animation via following properties and delegate 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; 
  2. ResizableTextView : This is again a UITextView subclass which encapsulate all size-changing related code.This is based on autolayout. ResizableTextView :这又是一个UITextView子类,它封装了所有与尺寸更改有关的代码。该子类基于自动布局。 This searches for height constraint added to textView and updates it with calculated content size. 这将搜索添加到textView的高度约束,并使用计算出的内容大小对其进行更新。 Here is the sample usage. 这是示例用法。

     [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 : This is one of the elegant solutions based in auto-layout. MBAutoGrowingTextView :这是基于自动布局的优雅解决方案之一。 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. 这个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 :This an NSObject subclass which encapsulated the code for resizing UITextView. GrowingTextViewHandler :这是一个NSObject子类,该子类封装了用于调整UITextView大小的代码。 This approach is based on autolayout. 这种方法基于自动布局。 Handling resize out of UITextView subclass gives much more flexibility. 从UITextView子类处理尺寸调整可提供更大的灵活性。 First, GrowingTextViewHandler does not have to worry about handling UITextViewDelegates. 首先,GrowingTextViewHandler不必担心处理UITextViewDelegates。 Second it will work for any UITextView subclass. 其次,它将适用于任何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 

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: 并查看它是否有效,如果不行,请尝试使用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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM