UITextView UITableViewCell sizeToFit无法按预期工作 [英] UITextView in UITableViewCell sizeToFit not working as expected

查看:87
本文介绍了UITextView UITableViewCell sizeToFit无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITextView ,我需要调整大小,使其宽度可以拉伸以填充屏幕,但可变高度,以便它足够大以显示所有文本。当调整大小时,它应该使 UITextView 最小可能的高度,以便它适合。

I have a UITextView that I need to resize so that it has a width that stretches to fill the screen but variable height so that it is just big enough to show all the text. When the resizing takes place it should make the UITextView the minimum possible height so that it fits the in.

In为了做到这一点,我一直在使用以下代码:

In order to do this I have been using the following code:

textView.text            = textMessage;
[textView sizeToFit];
CGRect rect              = textView.frame;
rect.size.height         = textView.contentSize.height;
textView.frame           = rect;

当我这样做时,似乎并没有使UITextView成为可能的最小高度。看来下面还剩下一些空间。这是一个例子(为了说明我的观点,我将背景颜色设置为红色):

When I do this it doesn't seem to make the UITextView the minimum height possible. It appears that there is some excess space left below. Here is an example (I have set the background colour to red in order to illustrate my point):

请问有人可以告知为什么会这样,以及我如何解决这个问题?请注意,UITextView位于自定义UITableView单元格中。此单元格使用自动调整蒙版水平拉伸以根据方向填充屏幕。

Please could someone advise why this is happening and how I can resolve the issue? Please note that the UITextView is located within a custom UITableView cell. This cell uses the Autoresizing masks to stretch horizontally to fill the screen depending on the orientation.

我花了很多年时间试图让它工作但我不明白为什么底部有多余的空间,当我旋转设备时,UITextViews的高度似乎都保持不变(即使我调用 [UITableView reloadData] 方法我还定义了行的高度方法。

I have spend ages trying to get this to work but I don't see why there is excess space at the bottom and also when I rotate the device the height of the UITextViews all seem to remain the same (even though I call the [UITableView reloadData] method which I have also defined the row heigh method for).

推荐答案

我遇到过同样的问题,发现了这个问题。

I had the same problem once and found this.

CGSize maximumLabelSize = CGSizeMake(300,80);//Or whatever size you need to be the max

CGSize expectedLabelSize = [textMessage sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeWordWrap];
rect.size.height = expectedLabelSize.height;
textView.frame = rect;

希望这会有所帮助。

这篇关于UITextView UITableViewCell sizeToFit无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆