Resizing UITextView

前端 未结 10 1385
隐瞒了意图╮
隐瞒了意图╮ 2020-12-04 15:52

I have a UITextView added on my UIView. The textview added is not editable, it is just to display some data. The data displayed in the textview is

相关标签:
10条回答
  • 2020-12-04 16:40

    Addressing the similar issue I just created a an auto-layout based light-weight UITextView subclass which automatically grows and shrinks based on the size of user input and can be constrained by maximal and minimal height - all without a single line of code.

    https://github.com/MatejBalantic/MBAutoGrowingTextView

    0 讨论(0)
  • 2020-12-04 16:41

    sizeToFit Does Work

    If you call sizeToFit after you set the text the first time it resizes. So after the first time you set it subsequent calls to set text will result in no change in size. Even if you call sizeToFit.

    However, you can force it to resize like this:

    1. Set the text.
    2. Change the textView frame height to be CGFLOAT_MAX.
    3. Call sizeToFit.
    0 讨论(0)
  • 2020-12-04 16:41

    Do the following:

    _textView.text = someText;
    [_textView sizeToFit];
    _textView.frame.height = _textView.contentSize.height;
    
    0 讨论(0)
  • 2020-12-04 16:42

    textView.contentSize.height in the textViewDidChange can only resize after text actually grows. For best visual result better to resize beforehand. After several hours I've figured out how to make it the same perfectly as in Instagram (it has the best algorithm among all BTW)

    Initialize with this:

        // Input
        _inputBackgroundView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, size.height - _InputBarHeight, size.width, _InputBarHeight)];
        _inputBackgroundView.autoresizingMask = UIViewAutoresizingNone;
       _inputBackgroundView.contentMode = UIViewContentModeScaleToFill;
        _inputBackgroundView.userInteractionEnabled = YES;
        [self addSubview:_inputBackgroundView];
       [_inputBackgroundView release];
    
       [_inputBackgroundView setImage:[[UIImage imageNamed:@"Footer_BG.png"] stretchableImageWithLeftCapWidth:80 topCapHeight:25]];
    
        // Text field
        _textField = [[UITextView alloc] initWithFrame:CGRectMake(70.0f, 0, 185, 0)];
       _textField.backgroundColor = [UIColor clearColor];
        _textField.delegate = self;
       _textField.contentInset = UIEdgeInsetsMake(-4, -2, -4, 0);
       _textField.showsVerticalScrollIndicator = NO;
       _textField.showsHorizontalScrollIndicator = NO;
        _textField.font = [UIFont systemFontOfSize:15.0f];
        [_inputBackgroundView addSubview:_textField];
       [_textField release];
    
       [self adjustTextInputHeightForText:@""];
    

    Fill UITextView delegate methods:

    - (void) textViewDidBeginEditing:(UITextView*)textView {
    
       [self adjustTextInputHeightForText:_textField.text];
    }
    
    - (void) textViewDidEndEditing:(UITextView*)textView {
    
       [self adjustTextInputHeightForText:_textField.text];
    }
    
    - (BOOL) textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text {
    
       if ([text isEqualToString:@"\n"])
       {
          [self performSelector:@selector(inputComplete:) withObject:nil afterDelay:.1];
          return NO;
       }
       else if (text.length > 0)
       {
          [self adjustTextInputHeightForText:[NSString stringWithFormat:@"%@%@", _textField.text, text]];
       }
       return YES;
    }
    
    - (void) textViewDidChange:(UITextView*)textView {
    
       [self adjustTextInputHeightForText:_textField.text];
    }
    

    And the trick is...

    - (void) adjustTextInputHeightForText:(NSString*)text {
    
       int h1 = [text sizeWithFont:_textField.font].height;
       int h2 = [text sizeWithFont:_textField.font constrainedToSize:CGSizeMake(_textField.frame.size.width - 16, 170.0f) lineBreakMode:UILineBreakModeWordWrap].height;
    
       [UIView animateWithDuration:.1f animations:^
       {
          if (h2 == h1)
          {
             _inputBackgroundView.frame = CGRectMake(0.0f, self.frame.size.height - _InputBarHeight, self.frame.size.width, _InputBarHeight);
          }
          else
          {
             CGSize size = CGSizeMake(_textField.frame.size.width, h2 + 24);
             _inputBackgroundView.frame = CGRectMake(0.0f, self.frame.size.height - size.height, self.frame.size.width, size.height);
          }
          CGRect r = _textField.frame;
          r.origin.y = 12;
          r.size.height = _inputBackgroundView.frame.size.height - 18;
          _textField.frame = r;
    
       } completion:^(BOOL finished)
       {
          //
       }];
    }
    
    0 讨论(0)
提交回复
热议问题