更多的文本和空白UITextView的底部 - More text and more blank at the bottom of UITextView

- 此内容更新于:2015-12-13
主题:

UITableViewControllerUITextView是在静态细胞,并添加代码,这样细胞高度可以适应文本。细胞工作好几个文本输入,当更多的文本,空白的底部的细胞变成了产率。我曾试图添加代码,而不是工作。任何帮助吗?谢谢。更新:奇怪的事情,ios8上运行时,它仍然显示的是空白,但当滚动到底部,这样细胞隐藏,然后滚动回到顶部,空白选项里!这意味着它在正常工作。是什么问题?

原文:

the UITextView is in a static cell in UITableViewController, and add code descriptionText.sizeToFit() so that the cell height can fit the text.

the cell works fine with few text input, when more text it gets, the blank at the bottom of the cell becomes heigher too.

I had tried to add code descriptionText.contentInset = UIEdgeInsetsMake(0, 0, 0, 0), not work either.

Any help? Thanks.

screenshot

Update: Weird things, when runs on ios8, it still shows the blank, BUT when scrolling to the bottom so that the cells hidden, and then scrolling back to top, THE BLANK DISPEARED! it means it works in normal. What's the problem?

解决方案:
你必须告诉视图不适合insets:文档说:如果你不?想要一个滚动视图?内容自动调整insets,automaticallyAdjustsScrollViewInsets没有设置。(默认值automaticallyAdjustsScrollViewInsets是肯定的。)所以你必须添加以下代码行viewDidLoad:或者你可以覆盖方法并返回。
原文:

You have to tell the view not to auto-adjust the insets:

The documentation says:

If you don’t want a scroll view’s content insets to be automatically adjusted, set automaticallyAdjustsScrollViewInsets to NO. (The default value of automaticallyAdjustsScrollViewInsets is YES.)

So you have to add the following line of code to your viewDidLoad:

self.automaticallyAdjustsScrollViewInsets = false;

Or you could just override the automaticallyAdjustsScrollViewInsets - method and return false.

楼主:我不得不说这工作太失败,尽管很多人说这个解决方案解决顶部的空白。当添加,顶部的细胞甚至隐藏导航栏下。你认为的insets的问题吗?奇怪,空白可以增加了文本。

(原文:I have to say it fails to work too, though many guys said this solution solve the blank at the top. When adding self.automaticallyAdjustsScrollViewInsets = false , the cell at the top even hidden under the navigation bar. Do you think it's a problem of insets? it's odd that the blank could be increased by the text.)

解决方案:
这里有一个方法:通过计算每个UITextView的高度和返回的高度(如果你想要+填充)方法。
原文:

Here's a way to do it: By calculating the height of each UITextView and returning the height (+padding if you want) in heightForRowAtIndexPath: method.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *body = self.messages[indexPath.row];
    CGRect rect = [body boundingRectWithSize:CGSizeMake([width of your UITextViews], CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue" size:[Font Size]} context:nil];
    return rect.size.height;
}
楼主:不。它不工作。约束之前设置,当我使用的时候,它只显示默认高度(30),不能显示整个文本。现在,细胞可以调整高度显示整个文本,但是还显示更多的空白。:-(

(原文:No. it doesn't work. constraints had been set before, when I use return UITableViewAutomaticDimension, it shows only the default height(30) which can't show the whole text. By now, the cell can adjust the height to show the whole text, but just also shows more blank. :-()

网友:我更新了我的回答与另一个方法。我自己也用这个它应该工作。

(原文:I updated my answer with another way to do it. I've used this myself and it should work.)

楼主:阿明,谢谢你的建议。当运行代码,第一次我有“CGFLOAT_MAX”不可用:使用CGFloat。马克斯,我使用“高度:10000.0。不能使用“|”迅速,所以只使用.UsesLineFragmentOrigin。这是工作!但是…取决于字体大小。字体大小设置为14磅,为了更好的textview高度,字体大小必须设置为12.5码。尽管这是一个更好的方法来解决这个问题,但我不认为这是最好的一个。

(原文:Armin, thanks your suggestion. When running the codes, first I got 'CGFLOAT_MAX' is unavailable: use CGFloat.max, so I use 'height: 10000.0 instead. and cannot use '|' in swift, so use only .UsesLineFragmentOrigin. It's work! BUT... depends on the font size. The font size is set to 14 in IB, for a better height of the textview, font size must be set to 12.5 in codes. Although it's a better way to solve the problem, but I don't think it's the best one.)

楼主:暂时使用来处理它。:-(

(原文:temporarily using height/1.22 to handle it. :-()

楼主:奇怪的事情,ios8上运行时,它仍然显示的是空白,但当滚动到底部,这样细胞隐藏,然后滚动回到顶部,空白选项里!这意味着它在正常工作。

(原文:Weird things, when runs on ios8, it still shows the blank, BUT when scrolling to the bottom so that the cells hidden, and then scrolling back to top, THE BLANK DISPEARED! it means it works in normal.)