2

I need to change UITextView Frame height dynamically with respect to content height of it. i.e. so when ever contain height is increased rather than allowing user to scroll i would like to change my UITextView's Frame height so users can read all data.

So here i my code snipper,

-(void)viewDidLoad{

    [super viewDidLoad];
    _txtView.delegate = self;
    previousheight = _txtView.contentSize.height;
    NSLog(@"%.2f",previousheight);
}

Here i am getting log value -> 22.

-(void)textViewDidChange:(UITextView *)textView{

    NSLog(@"%.2f",textView.contentSize.height);
}

Here i am getting log value -> 36.

===============================================================================

Now another way, if i disable scrolling of UITextView then i am getting perfect/expected result. Only thing is i am not able to enable scrolling afterward.

-(void)viewDidLoad{

    [super viewDidLoad];
    _txtView.scrollEnabled = FALSE;
    previousheight = _txtView.contentSize.height;
    NSLog(@"%.2f",previousheight);
}

Here i am getting log value -> 30.

-(void)textViewDidChange:(UITextView *)textView{

    NSLog(@"%.2f",textView.contentSize.height);
    txtView.scrollEnabled = TRUE;
}

Here i am getting log value -> 30.

But now there are no scrolling, although i have enable scrolling view.

Any help would be appreciated. Thanks

user2618646
  • 163
  • 1
  • 8

1 Answers1

0

I have reported this as a bug on iOS 7.0 to Apple yesterday.

I can calculate the text height correctly using 'boundingRectWithSize:...' but if scrolling is disabled the text gets cut off no matter the height.

A solution that sometimes works is:

textView.scrollEnabled = TRUE;
textView.text = @"Lorem ipsum...";
textView.scrollEnabled = FALSE;
Nikola Lajic
  • 3,985
  • 28
  • 34