UITextView change height instead of scroll

前端 未结 2 990
南方客
南方客 2020-12-15 01:33

By default, the UITextView\'s contentView becomes scrollable when there is too much text to fit into the textview based on it\'s height.

I\'d like to disable this an

相关标签:
2条回答
  • 2020-12-15 01:56

    It is very simply done like this:

    CGRect frame = textView.frame;
    
    frame.size = textView.contentSize;
    
    textView.frame = frame;
    

    This should set the height of the textview to appropriately fit all of its content.

    0 讨论(0)
  • 2020-12-15 02:06

    Few little changes:

    -(void)textViewDidChange:(UITextView *)textView {
    
        CGFloat fontHeight = (textView.font.ascender - textView.font.descender) + 1;
    
        CGRect newTextFrame = textView.frame;
        newTextFrame.size = textView.contentSize;
        newTextFrame.size.height = newTextFrame.size.height + fontHeight;
        textView.frame = newTextFrame;
    }
    

    Adding the font height gives room for the autocorrection box when you spell something incorrectly.

    The UITextView should also be set to not scroll:

    [aTextView setScrollEnabled:NO];
    
    0 讨论(0)
提交回复
热议问题