2014-11-04 103 views
0

我有一個禁用滾動的UITextView,我需要設置UITextView的高度以匹配文本內容的高度。我嘗試了各種方法找到了沒有結果的互聯網。這是我對的UITextView的佈局代碼:iOS:UITextView設置高度等於內容高度

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

我也試圖在viewDidAppear這段代碼:方法。有人可以使用自動佈局發佈iOS 7.0及更高版本的解決方案的代碼嗎?

+0

你有沒有在這些線路上設置斷點?他們被稱爲? – Neeku 2014-11-04 16:48:48

+0

是的,這些方法正在被調用。 – 2014-11-04 16:51:13

+0

我的意思是在這些方法的每一行中。 – Neeku 2014-11-04 16:51:44

回答

1

你試過嗎?我添加了一個按鈕,流動代碼在其動作方法中,我可以看到高度更改爲文本高度。

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

編輯

我添加了一個底部空間的SuperView約束,並設置爲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]; 
} 
+0

此答案無效。 UITextView的內容大小是正確的,但UITextView的實際高度不會使用此代碼更新 – 2014-11-05 14:50:11

+0

嗯,它確實爲我工作,我點擊按鈕,textView高度發生變化。 – gabbler 2014-11-05 14:52:57

+0

你在使用自動佈局嗎? – 2014-11-05 15:14:19

0

嘗試加入這一行到你的方法:

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

,看看它是否工作,如果不行,請嘗試使用UILabel代替UITextView,創下其numberOfLines0,並添加這個方法:

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

該方法無效。我必須使用UITextView,因爲我在某個時候啓用了滾動。 – 2014-11-04 17:15:06

+0

SO,第一種方法,將該行添加到您的方法中,不起作用? – Neeku 2014-11-04 17:24:32

+0

正確,我可以NSLog的contentSize和內容的高度是正確的,但UITextView的實際顯示高度不會更新到contentSize高度。我試過jobDescrip.frame = CGRectMake(x,y,width,jobDescrip.contentSize.height)沒有成功 – 2014-11-04 17:28:05

1

這些是處理在用戶輸入文本時調整UITextView大小的庫。

  1. CSGrowingTextView: 這是根據其內容,其調整大小自一個UITextView子類。這適用於自動佈局和非自動佈局。這通過以下屬性和委託方法提供了對調整動畫大小的更多控制。

    @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子類,封裝了所有大小變化的相關代碼。此是基於自動佈局。這將搜索添加到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:這是總部設在自動佈局優雅的解決方案之一。這個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:這一個NSObject子類,封裝的代碼調整大小的UITextView。這種方法基於自動佈局。處理調整大小的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