sirab333
sirab333

Reputation: 3722

iOS 7 UITextView height resize quirk

Using information I've gathered from multiple answers I found here on stackoverflow, I've been able to correctly size a UITextView's height using:

[bookQuoteTxtView sizeToFit];
[bookQuoteTxtView layoutIfNeeded];

I'm doing this because the UITextView will obviously contain different amounts of text as a result of what the user may have selected on a previous screen.

However, I'm still running into one strange problem.
It seems that when the text that gets passed into the UITextView contains a line-break, it throws the whole height thing off.

Here's the text without any line-breaks:

enter image description here

And here's the same text with a couple of line-breaks thrown in the middle:

enter image description here

As you can see (with the help of the orange & yellow background colors I added to the UITextView & ScrollView that contains it), the UITextView's height didn't resize appropriately and now the text gets cut off - all because of those linebreaks.

The code I'm using to do all this dynamic height-changing on the UITextView is:

// BookQuote TextView:
bookQuoteTxtView = [[UITextView alloc] initWithFrame:CGRectMake(5, 30, 310, 80)];
bookQuoteTxtView.backgroundColor = [UIColor orangeColor];
bookQuoteTxtView.font = [UIFont fontWithName:@"Arial" size:15];
bookQuoteTxtView.text = [NSString stringWithFormat:@"%@", bookObject.bookQuote];
[bookQuoteTxtView sizeToFit];
[bookQuoteTxtView layoutIfNeeded];
bookQuoteTxtView.editable = NO;

[scroller addSubview:bookQuoteTxtView];

// Now get the HEIGHT of the Book-Quote TextView:
CGRect textViewFrame = bookQuoteTxtView.frame;
CGFloat textViewFrameHeight = bookQuoteTxtView.contentSize.height;
textViewFrame.size.height = textViewFrameHeight;
bookQuoteTxtView.frame = textViewFrame;

So again, this works almost flawlessly for just about every string that gets passed into my UITextView box. Thus, different strings of different lengths all work, the UITextView's height adjusts automatically and everything's cool. Its just the line-breaks that throw everything off.

HAVING SAID THAT... Its seems that line-breaks generated using "\n" in the string DO work, but line breaks that are embedded/encoded in a string that's coming in and being read from an Sqlite3 database - do NOT.
So you gotta wonder if that's where the problem lies.

Here's the code I use to read my date from the Database:

  // Book Quote:
  char *bookQuoteChar = (char *)sqlite3_column_text(statement, 4);
  NSString *bookQuoteString;
  if (bookQuoteChar == NULL) {
      bookQuoteString = @"N/A";
  }
  else {
      bookQuoteString = [[NSString alloc] initWithUTF8String:bookQuoteChar];
  }

I'm thinking more and more that its the database that's causing the problem, but I'm presenting the entire picture here in its totality to make sure I'm still doing the height-readjustment stuff correctly.
Please let me know if something's jumping out at you or if there are any suggestions for fixes.

Upvotes: 4

Views: 2461

Answers (1)

Joey Clover
Joey Clover

Reputation: 776

I can't really suggest much besides using a technique to set the contentSize of the UITextView.

textView.text    = @"text from DB";
CGRect rect      = textView.frame;
rect.size.height = textView.contentSize.height;
textView.frame   = rect;

and then use any techniques that may be needed to additionally update the frame. Hope you solve the problem!

Upvotes: 1

Related Questions