How can I resize a UITextView programatically using AutoLayouts?

| | August 11, 2015

I have added a UITextView programatically using autolayouts. I want to resize UITextView based on the text size. I have tried some code, but it’s not working and I will appreciate it if anybody helps.

my code is below:-

Background class:-

#import "AutoGrowingTextView.h"

@implementation AutoGrowingTextView

- (void) layoutSubviews
{
    [super layoutSubviews];

    if (!CGSizeEqualToSize(self.bounds.size, [self intrinsicContentSize])) {
        [self invalidateIntrinsicContentSize];
    }
}

- (CGSize)intrinsicContentSize
{
    CGSize intrinsicContentSize = self.contentSize;

    // iOS 7.0+
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f) {
        intrinsicContentSize.width += (self.textContainerInset.left + self.textContainerInset.right ) / 2.0f;
        intrinsicContentSize.height += (self.textContainerInset.top + self.textContainerInset.bottom) / 2.0f;
    }

    return intrinsicContentSize;
}

@end

my main class

#import "ViewController.h"
#import "AutoGrowingTextView.h"

@interface ViewController ()

{
    AutoGrowingTextView * TextView;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    TextView = [AutoTextView new];
    TextView.translatesAutoresizingMaskIntoConstraints = NO;
    TextView.backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0];
    TextView.scrollEnabled = NO;

    [TextView layoutSubviews];

    [self.view addSubview:TextView];

    NSLayoutConstraint * constraint2 = [NSLayoutConstraint constraintWithItem:TextView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem: self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:20.0f];
    [self.view addConstraint:constraint2];

     constraint2 = [NSLayoutConstraint constraintWithItem:TextView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:5.0f];
    [self.view addConstraint:constraint2];

    constraint2 = [NSLayoutConstraint constraintWithItem:TextView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-5.0f];
    [self.view addConstraint:constraint2];

    constraint2 = [NSLayoutConstraint constraintWithItem:TextView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:50.0f];
    [self.view addConstraint:constraint2];

}

@end

One Response to “How can I resize a UITextView programatically using AutoLayouts?”

  1. This is going to sound a little nuts, but I’m fairly certain that if you remove the height constraint then the layout engine will size the UITextView to fit the font size. I’ve ran into this same problem just last week and this did the trick.

    Remove this constraint, and see if it works:

    constraint2 = [NSLayoutConstraint constraintWithItem:TextView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:50.0f];
    

    Let me add that you should define a font for the textView, that may be the winning ticket for you there on the resize

Leave a Reply