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

时间:2008-09-25 13:15:09

标签: iphone cocoa-touch uitextview

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

是否有人在UITextView内有UIScrollView解决方案或取得任何进展?感谢。

6 个答案:

答案 0 :(得分:7)

我解决了强制“假”卷轴的问题:

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

答案 1 :(得分:2)

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

是的,Safari会这样做,但必须这样做,而且这不是使用Safari最令人愉快的部分。

我认为这是困难表明你正在反对系统的那个时代之一。我强烈建议您回过头来重新思考用户界面。

答案 2 :(得分:1)

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

检查此页面的“屏幕外UITextViews无法正确更新”部分:Multiple Virtual Pages in a UIScrollView

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

答案 3 :(得分:0)

问题很可能就是UIScrollViews

我认为有三种解决方案:

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

答案 4 :(得分:0)

我对这个问题的解决方案有所不同:只有当我将UIScrollView的属性“Autoresize Subviews”设置为off时,它才有效。

[scrollView setAutoresizesSubviews:NO];

答案 5 :(得分:-1)

通过将文本值赋值放入 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模拟器中测试过)。

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