使用我从stackoverflow上找到的多个答案中收集的信息,我已经能够UITextView's
使用以下方法正确调整高度:
[bookQuoteTxtView sizeToFit];
[bookQuoteTxtView layoutIfNeeded];
我这样做是因为由于UITextView
用户可能在前一个屏幕上选择的内容,显然会包含不同数量的文本。
但是,我仍然遇到一个奇怪的问题。
似乎当传递给 的文本UITextView
包含换行符时,它会抛出整个高度。
这是没有任何换行符的文本:
这是相同的文本,中间有几个换行符:
正如您所看到的(借助我添加到包含它的UITextView
&的橙色和黄色背景颜色),高度没有适当调整大小,现在文本被截断 - 这一切都是因为这些换行符。ScrollView
UITextView's
我用来做所有这些动态高度变化的代码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;
再说一次,这几乎可以完美地适用于传递到我的UITextView
盒子的每个字符串。因此,不同长度的不同字符串都可以工作,UITextView's
高度会自动调整,一切都很酷。它只是把所有东西都扔掉的换行符。
话虽如此...似乎在字符串中使用“\ n”生成的换行符确实有效,但是嵌入/编码在进入并从Sqlite3数据库读取的字符串中的换行符 - 不要。
所以你想知道这是否是问题所在。
这是我用来从数据库中读取日期的代码:
// Book Quote:
char *bookQuoteChar = (char *)sqlite3_column_text(statement, 4);
NSString *bookQuoteString;
if (bookQuoteChar == NULL) {
bookQuoteString = @"N/A";
}
else {
bookQuoteString = [[NSString alloc] initWithUTF8String:bookQuoteChar];
}
我越来越多地认为它是导致问题的数据库,但我在这里完整地展示了整个图片,以确保我仍然正确地进行高度调整。
请让我知道您是否有任何问题或是否有任何修复建议。