代码之家  ›  专栏  ›  技术社区  ›  renesteg

如何在以编程方式更新数据后重新绘制iphone3.x的UITextView

  •  0
  • renesteg  · 技术社区  · 14 年前

    我正在处理长字符串,为什么我认为UITextView是最好的显示方式。但是,如果另一个UI类,比如UITextField或UILabel,能够更好、更容易地实现我想要的,我也会使用它。没问题。

    5 回复  |  直到 5 年前
        1
  •  0
  •   Shaggy Frog    14 年前

    你的问题是什么?

    你是在问如何在一个 UITextView text 属性:

    someTextView.text = @"Some Text";
    

    你是不是在问用一个 UILabel UITextField ? 那是个主观问题。这取决于你的应用程序是如何设计的,以及什么对呈现文本信息最有意义。(在 UIWebView

    一旦设置了这些内置类的文本,它就会知道如何更新/重画自己。您不需要设置文本,然后手动触发重绘或“重绘”。

        2
  •  2
  •   Phil Calvin    12 年前

    你可以强迫 UITextField 要通过设置文本颜色重新绘制文本,请执行以下操作:

    textField.textColor = [UIColor colorWithWhite:0.0 alpha:1.0];
    

    UIColor 实例。上面的代码可以做到这一点。如果已重写,则强制重新绘制可能很有用 drawTextInRect: 想要改变文本的外观而不改变文本本身。

    出于性能原因,请致电 [textField setNeedsDisplay] 不会重新绘制(或调用 drawTextInRect公司:

        3
  •  1
  •   Community CDub    7 年前

    我自己也在努力更新多行UITextView,我从 "Phil Calvin"

    在我的情况下,我用 textView.textColor = [UIColor blackColor];

    所以,这里有更多的上下文,可以帮助你完成任务。。。

    - (void)textViewDidChange:(UITextView *)textView
    {  
       // ... lots of code to calculate the correct autolayout constraints & content size
       [textView setContentSize:CGSizeMake(textFitSize.width, newTextContentHeight)];
    
       [textView.superview setNeedsUpdateConstraints];
    
       [UIView animateWithDuration:0.3
                         animations:^
                         {
                             // first apply the AutoLayout to resize the UITextView
                             [textView layoutIfNeeded];
                             [textView.superview layoutIfNeeded];
                         }
                         completion:^(BOOL finished)
                         {
                             // then update the text by resetting the text colour 
                             dispatch_async(dispatch_get_main_queue(), ^{
    
                                 [textView.textColor = [UIColor blackColor];
                                 [UIView animateWithDuration:0.3
                                      animations:^(void) {                                   
                                         // do not loose the keyboard
                                         [textView becomeFirstResponder];
                                      }
                                      completion:^(BOOL finished) {
                                         // move the cursor to the bottom of the text view
                                         textView.contentOffset = CGPointMake(0., textView.contentSize.height);
                                  }];
                             });
          }];
    }           
    
        4
  •  0
  •   samkass    14 年前

    [myTextView setNeedsDisplay]; 工作?这是告诉iOS UIView需要重新绘制它自己的传统方法。这个 UIView documentation 包含了一些很好的信息。

        5
  •  0
  •   renesteg    14 年前

    看起来,我对我最初的问题描述不是很清楚,这里是功能描述: 我需要我的应用程序在UITextView中显示SQLITE3数据库中的文本,并每隔2秒用DB中下一条记录中的文本更新该文本。所以我最初的想法是在Select循环中进行。有了shaggy的提示,视图不会被更新,在完成循环之前,因为它是在一个线程中运行的,我开始寻找另一个解决方案,并提出了以下基于选择器的解决方案,效果非常好。

    - (IBAction)displayTextFromArray:(UIButton *)sender{
    timer = [NSTimer scheduledTimerWithTimeInterval:(2.0) target:self selector:@selector(displayText) userInfo:nil repeats:YES];    
    

    }

    在我的例子中,这个方法是在用户触碰一个ui按钮时调用的。要使计划停止,需要向计时器实例发送以下消息:

            [timer invalidate];
    

    干杯,任