UITextView 中的文本不会显示在 UIScrollView 中

发布于 2024-07-05 08:03:32 字数 326 浏览 4 评论 0原文

我想要一个带有一组子视图的 UIScrollView,其中每个子视图都有一个带有不同文本的 UITextView。 对于此任务,我修改了苹果“iphone 开发中心”的 PageControl 示例,以便将其添加到用于生成子视图的简单 UITextView 视图的滚动视图。 当我运行应用程序(在模拟器和手机上)时,看不到任何文本,但如果我激活“用户交互”并单击它,文本就会神奇地出现(以及键盘)。

有没有人有解决方案或在 UIScrollView 内使用 UITextView 取得任何进展? 谢谢。

I want to have a UIScrollView with a set of subviews where each of these subviews has a UITextView with a different text. For this task, I have modified the PageControl example from the apple "iphone dev center" in order to add it a simple UITextView to the view which is used to generate the subviews of the scroll view. When I run the app (both on the simulator and the phone), NO Text is seen but if i activate the "user interaction" and click on it, the text magically appears (as well as the keyboard).

Does anyone has a solution or made any progress with UITextView inside a UIScrollView? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

枫林﹌晚霞¤ 2024-07-12 08:03:32

它通过将文本值分配放入 scrollViewDidScroll 方法中来对我起作用。

示例片段:


SAMPLE.h

...
@interface myRootUIViewController : UIViewController <UIScrollViewDelegate>
...

注释:
请记住:不要忘记 UIScrollViewDelegate 协议。


SAMPLE.m

    - (void)viewDidLoad {
       ... whatever is created before and/or after...

       NSString * text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
                           Nunc semper lacus quis erat. Cras sapien magna, porta non, 
                           suscipit nec, egestas in, arcu. Maecenas sit amet est. 
                           Quisque felis risus, tempor eu, dictum ac, volutpat id, 
                           libero. Ut gravida, purus vitae interdum elementum, tortor 
                           justo porttitor nisi, id rhoncus massa.";

       // calculate the required frame height according to defined font size and
       // given text
       CGRect frame = CGRectMake(0.0, 500.0, self.view.bounds.size.width, 1000.0); 
       CGSize calcSize = [text sizeWithFont:[UIFont systemFontOfSize:13.0]
              constrainedToSize:frame.size lineBreakMode: UILineBreakModeWordWrap];
              // for whatever reasons, contraintedToSize seem only be able to
              // calculate an appropriate height if the input frame height is larger
              // than required. Means: if your text requires height=250 and input
              // frame height=100, then this method won't give you the expected
              // result.

       frame.size = calcSize;
       frame.size.height += 0; // calcSize might be not pixel-precise, 
                               // so add here additional padding pixels
       UITextView * tmpTextView = [[UITextView alloc]initWithFrame:frame];

       // do whatever adjustments
       tmpTextView.backgroundColor = [UIColor blueColor]; // show area explicitly (dev 
                                                          // purpose)
       self.myTextView = tmpTextView;
       self.myTextView.editable = NO;
       self.myTextView.scrollEnabled = NO;
       self.myTextView.multipleTouchEnabled = NO;
       self.myTextView.userInteractionEnabled = NO; // pass on events to parentview
       self.myTextView.font = [UIFont systemFontOfSize:13.0];
       [tmpTextView release];
       [self.scrollView addSubview:self.myTextView];
}

...

- (void)scrollViewDidScroll:(UIScrollView *)sender {
  // for simplicity text is repeated again, of course it can be a member var/etc...
  NSString * text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
                           Nunc semper lacus quis erat. Cras sapien magna, porta non, 
                           suscipit nec, egestas in, arcu. Maecenas sit amet est. 
                           Quisque felis risus, tempor eu, dictum ac, volutpat id, 
                           libero. Ut gravida, purus vitae interdum elementum, tortor 
                           justo porttitor nisi, id rhoncus massa.";
  self.myTextView.text = text; // assign value within this method and it is
                               // painted as expected.
    }

评论:

我显然已经使用示例命名和值调整了源代码片段。 希望没有错别字。 但是,代码还包含文本所需框架高度的计算,以防文本的值可能发生变化,因此实际上需要不同的框架大小。

将实际的文本值分配到scrollViewDidScroll方法中对我来说很有效,在滚动等过程中没有任何类型的闪烁(到目前为止仅在iPhone模拟器中进行了测试)。

希望有帮助。 当然,我愿意接受任何建设性的反馈、改进建议,甚至其他解决此问题的方法。

it works for me by placing the text value assignment into the scrollViewDidScroll method.

Sample snippets:


SAMPLE.h

...
@interface myRootUIViewController : UIViewController <UIScrollViewDelegate>
...

Comment:
Just to remember: don't forget the UIScrollViewDelegate protocol.


SAMPLE.m

    - (void)viewDidLoad {
       ... whatever is created before and/or after...

       NSString * text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
                           Nunc semper lacus quis erat. Cras sapien magna, porta non, 
                           suscipit nec, egestas in, arcu. Maecenas sit amet est. 
                           Quisque felis risus, tempor eu, dictum ac, volutpat id, 
                           libero. Ut gravida, purus vitae interdum elementum, tortor 
                           justo porttitor nisi, id rhoncus massa.";

       // calculate the required frame height according to defined font size and
       // given text
       CGRect frame = CGRectMake(0.0, 500.0, self.view.bounds.size.width, 1000.0); 
       CGSize calcSize = [text sizeWithFont:[UIFont systemFontOfSize:13.0]
              constrainedToSize:frame.size lineBreakMode: UILineBreakModeWordWrap];
              // for whatever reasons, contraintedToSize seem only be able to
              // calculate an appropriate height if the input frame height is larger
              // than required. Means: if your text requires height=250 and input
              // frame height=100, then this method won't give you the expected
              // result.

       frame.size = calcSize;
       frame.size.height += 0; // calcSize might be not pixel-precise, 
                               // so add here additional padding pixels
       UITextView * tmpTextView = [[UITextView alloc]initWithFrame:frame];

       // do whatever adjustments
       tmpTextView.backgroundColor = [UIColor blueColor]; // show area explicitly (dev 
                                                          // purpose)
       self.myTextView = tmpTextView;
       self.myTextView.editable = NO;
       self.myTextView.scrollEnabled = NO;
       self.myTextView.multipleTouchEnabled = NO;
       self.myTextView.userInteractionEnabled = NO; // pass on events to parentview
       self.myTextView.font = [UIFont systemFontOfSize:13.0];
       [tmpTextView release];
       [self.scrollView addSubview:self.myTextView];
}

...

- (void)scrollViewDidScroll:(UIScrollView *)sender {
  // for simplicity text is repeated again, of course it can be a member var/etc...
  NSString * text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
                           Nunc semper lacus quis erat. Cras sapien magna, porta non, 
                           suscipit nec, egestas in, arcu. Maecenas sit amet est. 
                           Quisque felis risus, tempor eu, dictum ac, volutpat id, 
                           libero. Ut gravida, purus vitae interdum elementum, tortor 
                           justo porttitor nisi, id rhoncus massa.";
  self.myTextView.text = text; // assign value within this method and it is
                               // painted as expected.
    }

Comment:

I have adjusted the source code snippet with sample namings and values obviously. Hopefully there is no typo. However, the code contains also the calculation of the required frame height for the text, in case the text's value can change and therefore would require different frame sizes actually.

Placing the actual text value assignment into the scrollViewDidScroll method worked for me without any kind of flashing during scrolling etc. (so far only tested in iPhone Simulator).

Hope that helps. Of course I am open for any constructive feedback, improvement proposals or even other ways to solve this isuse.

最后的乘客 2024-07-12 08:03:32

我对这个问题的解决方案是不同的:只有当我将 UIScrollView 的属性“Autoresize Subviews”设置为关闭时,它才起作用。

[scrollView setAutoresizesSubviews:NO];

My solution to this problem was different: It only worked when I set the property "Autoresize Subviews” of the UIScrollView to off.

[scrollView setAutoresizesSubviews:NO];
公布 2024-07-12 08:03:32

问题很可能出在下一个 UIScrollViews 上。

我认为存在三种解决方案:

  • 在选择各种控件时启用和禁用 userInteractionEnabled
  • 假设文本是静态的,请使用 UILabel 而不是 UITextViewUILabel 不是 UIScrollView 的子类)
  • 实现您自己的视图并在drawRect消息中自己绘制文本,而不是依赖UITextView

The problem is likely to be the nexted UIScrollViews.

I think there are three solutions:

  • Enable and disable userInteractionEnabled as the various controls are selected.
  • Assuming the text is static, use a UILabel instead of a UITextView (UILabel is not a subclass of UIScrollView)
  • Implement your own view and draw the text yourself in a drawRect message rather than relying on the UITextView.
傾旎 2024-07-12 08:03:32

您可能会遇到 UITextView 从屏幕外滚动到屏幕上区域时无法正确更新的问题。

检查本页的“屏幕外 UITextView 未正确更新”部分: UIScrollView 中的多个虚拟页面

我使用的解决方案是当滚动视图开始出现在屏幕上时强制重新绘制滚动视图。 这完全是一个麻烦,但确实解决了问题。

You may be suffering from the problem where UITextView's don't update properly when they are scrolled from an offscreen to an onscreen area.

Check the "Offscreen UITextViews don't update correctly" section of this page: Multiple Virtual Pages in a UIScrollView

The solution I used was to force a redraw of scroll views when they begin to appear onscreen. This is a complete nuisance but does fix the problem.

╰沐子 2024-07-12 08:03:32

我解决了强制“假”滚动的问题:

textView.contentOffset = CGPointMake(0, 1);
textView.contentOffset = CGPointMake(0, 0);

I resolved the problem forcing a "fake" scroll:

textView.contentOffset = CGPointMake(0, 1);
textView.contentOffset = CGPointMake(0, 0);
请帮我爱他 2024-07-12 08:03:32

我认为问题源于 UITextViewUIScrollView 的子类,因此基本上将滚动视图嵌入到 UIScrollViews 中。 即使文本显示正确,您也会遇到可用性问题,因为永远不清楚手指滑动是否应该滚动外部视图或文本视图。

是的,Safari 确实做到了这一点,但它必须这样做,而且这并不是使用 Safari 中最令人愉快的部分。

我认为这是困难表明你正在对抗系统的时候之一。 我强烈建议回去重新考虑用户界面。

I think the problem stems from the fact that UITextView is a subclass of UIScrollView, so you basically have scroll views embedded within UIScrollViews. Even if the text displayed properly, you would have usability problems, as it would never be clear if a finger swipe was supposed to scroll the outer view or the text view.

Yeah, Safari sort of does this, but it has to, and it's not the most pleasant part of using Safari.

I think this is one of those times where the difficulty indicates you are working against the system. I strongly recommend going back and re-thinking the UI.

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