简体   繁体   English

iOS 7 UITextView高度调整大小怪异

[英]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: 使用我从stackoverflow上找到的多个答案中收集的信息,我已经能够使用以下方法正确调整UITextView's高度:

[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. 我这样做是因为UITextView显然会包含不同数量的文本,因为用户可能在之前的屏幕上选择了该文本。

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. 似乎当传入UITextView的文本包含换行符时,它会抛出整个高度的东西。

Here's the text without any line-breaks: 这是没有任何换行符的文本:

在此输入图像描述

And here's the same text with a couple of line-breaks thrown in the middle: 这里有相同的文字,中间有几个换行符:

在此输入图像描述

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. 正如你所看到的(在我添加到包含它的UITextViewScrollView的橙色和黄色背景颜色的帮助下), UITextView's高度没有适当调整大小,现在文本被切断 - 所有这些都是因为那些换行符。

The code I'm using to do all this dynamic height-changing on the UITextView is: 我用来在UITextView上进行所有动态高度变换的代码是:

// 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. 因此,对于几乎所有传递到我的UITextView框中的字符串,这几乎完美无缺。 Thus, different strings of different lengths all work, the UITextView's height adjusts automatically and everything's cool. 因此,不同长度的不同字符串都可以工作, UITextView's高度自动调整,一切都很酷。 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. 具有这样的意思......看起来在字符串DO中使用“\\ n”生成的换行符,但是在进入并从Sqlite3数据库读取的字符串中嵌入/编码的换行符 - 不要。
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. 如果有什么东西向您发出或者有任何修复建议,请告诉我。

I can't really suggest much besides using a technique to set the contentSize of the UITextView. 除了使用一种技术来设置UITextView的contentSize之外,我无法提出太多建议。

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! 希望你解决问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM