自动调整 UITextView 和 UIScrollView 的大小

发布于 2024-08-19 12:03:40 字数 919 浏览 1 评论 0原文

我已经在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一绘本一梦想 2024-08-26 12:03:40

实际上有一种非常简单的方法可以使用其 contentSize 将 UITextView 的大小调整到正确的高度。

CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;

需要注意的一件事是,只有在使用 addSubview 将 UITextView 添加到视图之后,正确的 contentSize 才可用。在此之前它等于frame.size

There is actually a very easy way to do resize the UITextView to its correct height using its contentSize.

CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;

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

围归者 2024-08-26 12:03:40

UITextView 不会自动根据其内容调整自身大小(据我所知无论如何),因此您需要自己获取文本的大小并相应地调整 UIView 的大小。

此页面上的函数将帮助 - 您可以使用它们来获取字符串的宽度和高度,例如

// Get the size that the string will render at
NSString *contents = @"This is the content of your UITextView";
CGSize areaSize = [contents sizeWithFont:myView.font forWidth:myView.frame.size.width lineBreakMode:UILineBreakModeWordWrap];

// Then set the frame of your UITextView to be the right size
myView.bounds = CGRectMake(0, 0, areaSize.width, areaSize.height);

然后,您可以在其周围布局其他组件。

希望这有帮助,

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

// Get the size that the string will render at
NSString *contents = @"This is the content of your UITextView";
CGSize areaSize = [contents sizeWithFont:myView.font forWidth:myView.frame.size.width lineBreakMode:UILineBreakModeWordWrap];

// Then set the frame of your UITextView to be the right size
myView.bounds = CGRectMake(0, 0, areaSize.width, areaSize.height);

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文