3

I have read several solutions on this site and tried them all, but still haven't had luck with them. Currently my test is a single viewController with an UITextView on it. I am trying to have the height of my UITextView adjust based on the text size.

The good: The solution referenced below seems to be giving me the correct height to set in my UITextView.

"Link"

The bad: After getting the ideal height back, it seems that the UITextView won't allow the height to be set. The code below shows a couple of different attempts I have used to accomplish this. Currently the third try method seems to be getting the closest, but it's still leaving me with an fixed height UITextView.enter image description here

Here is the output of my most recent logs from the thirdtry. It seems everything is working, but the UITextView is not getting resized.

2013-10-11 08:27:03.374 textexpand[25273:a0b] The bounds height 872.000000
2013-10-11 08:27:03.377 textexpand[25273:a0b] The frame height 872.000000
2013-10-11 08:27:03.378 textexpand[25273:a0b] The frame height 872.000000
2013-10-11 08:27:03.380 textexpand[25273:a0b] The frame height 785.000000
2013-10-11 08:27:03.381 textexpand[25273:a0b] The bounds height 785.000000

Here is the code:

- (void)viewDidLoad
{
[super viewDidLoad];

[self thirdtry];

}

////

- (void) firsttry
{
NSString *theText = @"Here is the text I am going to load into the textview.   The text view should automatically expand the height based on the size of the content, but leave the width static.   Also, I would ideally add my own padding to the bottom and top of the uitext view. More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text";

_theTextView.text = theText;

_theTextView.scrollEnabled = NO;
[_theTextView sizeToFit];
[_theTextView layoutIfNeeded];

CGRect frame = _theTextView.frame;
frame.size.height = _theTextView.contentSize.height;
_theTextView.frame = frame;

}

////

- (void) secondtry
{
NSString *theText = @"Here is the text I am going to load into the textview.   The text view should automatically expand the height based on the size of the content, but leave the width static.   Also, I would ideally add my own padding to the bottom and top of the uitext view. More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text";

NSAttributedString *attText = [[NSAttributedString alloc] initWithString:theText attributes:nil];
CGRect frame = _theTextView.frame;
CGFloat myfloat = [self textViewHeightForAttributedText:attText andWidth:100];
NSLog(@"The float is %f", myfloat);
frame.size.height = [self textViewHeightForAttributedText:attText andWidth:100];
_theTextView.frame = frame;


}

////

- (void) thirdtry
{

_theTextView.scrollEnabled = NO;

NSString *theText = @"Here is the text I am going to load into the textview.   The text view should automatically expand the height based on the size of the content, but leave the width static.   Also, I would ideally add my own padding to the bottom and top of the uitext view. More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text";

NSAttributedString *attText = [[NSAttributedString alloc] initWithString:theText attributes:nil];

//Attempt to adjust the bounds
CGRect bounds = _theTextView.bounds;
bounds.size.height = [self textViewHeightForAttributedText:attText andWidth:100];
NSLog(@"The bounds height %f", bounds.size.height);
_theTextView.bounds = bounds;

//Attempt to adjust the frame
CGRect frame = _theTextView.frame;
frame.size.height = [self textViewHeightForAttributedText:attText andWidth:100];
_theTextView.frame = frame;
NSLog(@"The frame height %f", frame.size.height);

NSLog(@"The frame height %f", _theTextView.frame.size.height);
[_theTextView sizeToFit];
NSLog(@"The frame height %f", _theTextView.frame.size.height);
}

////

- (CGFloat)textViewHeightForAttributedText: (NSAttributedString*)text andWidth: (CGFloat)width
{
UITextView *calculationView = [[UITextView alloc] init];
[calculationView setAttributedText:text];
CGSize size = [calculationView sizeThatFits:CGSizeMake(width, FLT_MAX)];
return size.height;

}

Update:

I should add that I can get the desired effect by initializing the uitextbox inside my code using this approach:

CGFloat myfloat= [self textViewHeightForAttributedText:attText andWidth:300];
_theTextView = [[UITextView alloc] initWithFrame:CGRectMake(350, 100, 300, myfloat)];
_theTextView.text = theText;
[_theview addSubview:_theTextView];

However, I would prefer not to do it this way and adjust the frame defined in IB on the fly.

Community
  • 1
  • 1
mday
  • 427
  • 1
  • 7
  • 22

1 Answers1

0

Here's a github repo that may help you: resizableTextView

acecapades
  • 893
  • 12
  • 23