1
votes

I have a UITextView with Autolayout ON inside storyboard view controller. I want to increase its height dynamically according to text of the UITextView. I'm able to increase its size but there is extra space below text. How can I remove this extra space?

I tried these lines of code:

[_descriptionText setText:string]; 
CGSize sizeThatFitsTextView = [_descriptionText sizeThatFits:CGSizeMake(_descriptionText.frame.size.width, MAXFLOAT)]
_descriptionTextHeight.constant = sizeThatFitsTextView.height;

Here _descriptionText is a UITextView and _descriptionTextHeight is a height constraint which I'm using to resize the UITextView.

I've also tried these lines of code:

[_descriptionText setText:string];
_descriptionTextHeight.constant = [_descriptionText intrinsicContentSize].height;

I was able to remove that extra space but UITextView size was not increasing. One more point I want to add here, I don't want to enable scroll.

enter image description here

3
Thanks for suggestion matt. I can subtract, Its working but the problem is text is not going to be fixed. It increases that bottom space according to content. Suppose if there are 10 text lines then there will be more space. So subtracting with constant value will not help.user4967912

3 Answers

0
votes

try this, must work for you.

CGSize sizeThatFitsTextView = [txt sizeThatFits:CGSizeMake(txt.frame.size.width, MAXFLOAT)];

_descriptionText.contentOffset = CGPointZero;
_descriptionText.contentInset = UIEdgeInsetsMake(-6,0,0,0);
[txt layoutIfNeeded]; //added

NSLayoutConstraint *heightConstraint;
for (NSLayoutConstraint *constraint in _descriptionText.constraints) {
    if (constraint.firstAttribute == NSLayoutAttributeHeight) {
        heightConstraint = constraint;
        break;
    }
}
heightConstraint.constant = sizeThatFitsTextView.height-12;


[_descriptionText layoutIfNeeded];
0
votes

I don't know why you're having difficulties with this. The following code works for me:

func adjustHeight(_ tv:UITextView) {
    self.heightConstraint.constant = ceil(tv.contentSize.height)
}

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    self.adjustHeight(textView)
    return true
}
0
votes

Create a subclass of UITextView and add this code:

- (void)layoutSubviews
{
    [super layoutSubviews];

    // Layout subviews gets called when the text view's text changes in a way that might
    // require scrolling.   We get its new intrinsic size and update the height constraint
    // to grow the text view.
    CGSize intrinsicSize = self.intrinsicContentSize;
    self.heightConstraint.constant = MAX(intrinsicSize.height, self.minimumHeight);

    // The text view has a tendency to scroll up once it's full, but before the
    // height constraint that increases its height takes effect.   This results
    // in a blank area at the bottom, with the first line pushed out of view.
    // We cancel the scroll action, but only if it has not reached its maximum height.
    if (intrinsicSize.height < self.maximumHeight) {
        self.contentOffset = CGPointMake(0, 0);
    }
}

You'll also need to create these properties. These control the height of the text view and sets limits on how short and how tall it can get.

/**
 The height constraint for the text view installed in the text view's superview.
 */
@property (weak,nonatomic) NSLayoutConstraint *heightConstraint;

/**
 The text view's minimum height.
 */
@property (nonatomic) CGFloat minimumHeight;

/**
 The text view's maximum height.
 */
@property (nonatomic) CGFloat maximumHeight;

Add the text view to your view hierarchy as usual, create a constraint for the text view's height, and set its min and max height properties and you're all set.