1

enter image description here

I want to increase textview height based on text-length i tried some code but result is coming like first image but i want to come the result like second image.

code:

- (void)viewDidLoad {
    [super viewDidLoad];

    textview.text = @"This returns the size of the rectangle that fits the given string with the given font. Pass in a size with the desired width and a maximum height, and then you can look at the height returned to fit the text. There is a version that lets you specify line break mode also.This returns the size of the rectangle that fits the given string with the given font. Pass in a size with the desired width and a maximum height, and then you can look at the height returned to fit the text. There is a version that lets you specify line break mode yes.";

    CGRect frame = textview.frame;
    frame.size.height = textview.contentSize.height;
    textview.frame = frame;
    textview.ScrollEnabled = NO;
}

enter image description here

Bista
  • 7,869
  • 3
  • 27
  • 55

3 Answers3

1

Might be help you. It's a Github controller

kb920
  • 3,039
  • 2
  • 33
  • 44
1

Take a look at this example. The basic idea is to isolate resizing code from controllers. The code in example can be rewritten:

     override func updateConstraints() {

        let contentSize = sizeThatFits(CGSizeMake(frame.size.width, CGFloat.max))

        for constraint in constraints() {

            if constraint.isKindOfClass(NSLayoutConstraint) {      
                let constraint = constraint as! NSLayoutConstraint
                if constraint.firstAttribute == NSLayoutAttribute.Height {
                    constraint.constant = contentSize.height  
                }
            }

        }

        super.updateConstraints()

    }
Nikita Took
  • 3,980
  • 25
  • 33
0

If you are using constraints (and you should), just don't set any constraints for the height and disable the scroll. This will automatically update the text view's height according to its content.

Jaeger
  • 324
  • 1
  • 10