自动调整 UITextView 和 UIScrollView 的大小
我已经在 Stack Overflow 和 Google 上进行了多次搜索,但找不到解决我的问题的方法。
我有一个产品的详细视图,其中包括 UIScrollView 和一些子视图(UIImageView、UILabel、UITextView)。您可以在此处找到示例。
首先,我想自动调整 UITextView (不是文本本身!)的大小到相应的高度。然后我想自动调整整个 UIScrollView 的大小,使其适合我的 UITextView 及其上方元素的高度。我尝试了以下代码:
myUITextView.frame = CGRectMake(2.0, 98.0, 316.0, self.view.bounds.size.height);
[scrollView setContentOffset:CGPointMake(0.0, 0.0) animated:NO];
scrollView.contentSize = CGSizeMake(320.0, 98.0 + [myUITextView frame].size.height);
[self.view addSubview:scrollView];
98.0 + [myUITextView框架].size.height)因为我的想法是:在获取myUITextView的高度后,将其他子视图的高度(98.0)添加到它。
不幸的是它的效果不太好。根据 UIScrollView 的内容,它太短或太长。我做了一些记录,结果 UITextView 的高度始终相同:
2010-01-27 14:15:45.096 myApp[1362:207] [myUITextView 框架].size.height: 367.000000
谢谢!
I already did several searches on Stack Overflow and Google, but I couldn't find a solution to my problem.
I have a Detail View for a product that includes a UIScrollView and a few subviews (UIImageView, UILabel, UITextView). You can find an example here.
First I wanna autoresize the UITextView (not the text itself!) to the corresponding height. Then I wanna autoresize the entire UIScrollView so that it fits the height of my UITextView and the elements above it. I've tried the following code:
myUITextView.frame = CGRectMake(2.0, 98.0, 316.0, self.view.bounds.size.height);
[scrollView setContentOffset:CGPointMake(0.0, 0.0) animated:NO];
scrollView.contentSize = CGSizeMake(320.0, 98.0 + [myUITextView frame].size.height);
[self.view addSubview:scrollView];
98.0 + [myUITextView frame].size.height) because my thought was: After getting the height of myUITextView, add the height of the other subviews (98.0) to it.
Unfortunately it doesn't work very well. Depending on the content of the UIScrollView, it is too short or too long. I did some logging with the result that the height of the UITextView is always the same:
2010-01-27 14:15:45.096 myApp[1362:207] [myUITextView frame].size.height: 367.000000
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上有一种非常简单的方法可以使用其 contentSize 将 UITextView 的大小调整到正确的高度。
需要注意的一件事是,只有在使用 addSubview 将 UITextView 添加到视图之后,正确的 contentSize 才可用。在此之前它等于frame.size
There is actually a very easy way to do resize the UITextView to its correct height using its contentSize.
One thing to note is that the correct contentSize is only available after the UITextView has been added to the view with addSubview. Prior to that it is equal to frame.size
UITextView 不会自动根据其内容调整自身大小(据我所知无论如何),因此您需要自己获取文本的大小并相应地调整 UIView 的大小。
此页面上的函数将帮助 - 您可以使用它们来获取字符串的宽度和高度,例如
然后,您可以在其周围布局其他组件。
希望这有帮助,
S
PS 警告,文档的链接是正确的,但我的代码示例未经测试,抱歉:)
A UITextView won't automatically resize itself to it's contents (not as far as I know anyway) so you need to get the size of the text yourself and size the UIView accordingly.
The functions on this page will help - you can use them to get the width and height of a string, something like
Then, you can layout the other components around it.
Hope this helps,
S
PS Warning, the link to the docs is correct but my code example is untested, sorry :)