UITextView中的文本不显示在UIScrollView中

时间:2020-03-06 14:43:11  来源:igfitidea点击:

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

是否有人在UIScrollView内部使用UITextView解决方案或者取得了任何进展?谢谢。

解决方案

我认为问题源于UITextView是UIScrollView的子类,因此我们基本上在UIScrollViews中嵌入了滚动视图。即使文本显示正确,我们也会遇到可用性问题,因为如果用手指轻扫滚动外部视图或者文本视图,就永远不会很清楚。

是的,Safari可以做到这一点,但这是必须的,这并不是使用Safari最令人愉快的部分。

我认为这是困难表明我们正在使用该系统的情况之一。我强烈建议我们返回并重新考虑UI。

它通过将文本值分配放入scrollViewDidScroll方法为我工作。

样本片段:

样本

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

评论:
请记住:不要忘记UIScrollViewDelegate协议。

样例

- (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 Simulator中进行了测试)。

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

问题可能出在接下来的UIScrollViews中。

我认为有三种解决方案:

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

当UITextView从屏幕外滚动到屏幕区域时,UITextView的更新可能无法正确进行。

检查此页面的"屏幕外UITextViews无法正确更新"部分:UIScrollView中的多个虚拟页面

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