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

发布于 2024-09-19 06:29:29 字数 249 浏览 12 评论 0原文

在我的应用程序中,我以编程方式更新文本视图。实际上,我循环访问 SQLITE3 DB 中的数据集并显示给定时间的特定文本。然后我想显示下一个数据集记录中的文本等等。我一直在浏览各种论坛和苹果文档,但找不到诸如重绘、刷新、updateTextView 等命令之类的东西。 我正在处理长字符串,为什么我认为 UITextView 将是最好的显示方式。但是,如果另一个 UI 类(例如 UITextField 或 UILabel)会更好、更容易实现我正在寻找的目标,我也会使用它。没问题。

In my app I update a textView programmatically. I actually loop through a dataset from a SQLITE3 DB and show a specific text for a given time. Then I would like to show the text from the next dataset record and so forth. I have been browsing through all kind of forums and the apple doc as well, but could not find anything like a repaint, refresh, updateTextView or so command.
I am dealing with long strings, why I thought the UITextView would be the best way to display. However, if another UI Class, like the UITextField or UILabel would be better and easier to achieve what I am looking for, I would also use that. No problem.

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

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

发布评论

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

评论(5

拥醉 2024-09-26 06:29:30

您可以通过设置文本颜色来强制 UITextField 重新绘制其文本:

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

颜色不需要不同,它只需要是一个新的 UIColor 实例即可。上面的代码将执行此操作。如果您已覆盖 drawTextInRect: 并且想要更改文本的外观而不更改文本本身,则强制重绘可能会很有用。

出于性能原因,如果文本未更改,则调用 [textField setNeedsDisplay] 将不会重新绘制(或调用 drawTextInRect:)。

You can force a UITextField to repaint its text by setting its text color:

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

The color doesn't need to be different, it simply needs to be a new UIColor instance. The above code will do this. Forcing a repaint can be useful if you have overridden drawTextInRect: and want to change the appearance of the text without changing the text itself.

For performance reasons, calling [textField setNeedsDisplay] will not repaint (or call drawTextInRect:) if the text has not changed.

月下客 2024-09-26 06:29:30

我自己正在努力更新多行 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);
                              }];
                         });
      }];
}           

I was struggling with updating multiline UITextView myself, and I found a solution from the answer of "Phil Calvin"

In my case I used textView.textColor = [UIColor blackColor];

So, here's some more context that may help you in your task...

- (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);
                              }];
                         });
      }];
}           
萌辣 2024-09-26 06:29:30

你的问题到底是什么?

您是否想问如何在 UITextView 中设置文本?只需使用其 text 属性:

someTextView.text = @"Some Text";

您是否询问使用 UILabelUITextField 是否“更好”?这是一个主观问题。这取决于您的应用程序的设计方式以及呈现文本信息最有意义的方式。 (在 UIWebView 中呈现格式化的 HTML 是另一种选择。)

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

What exactly is your question?

Are you asking how to set text in a UITextView? Just use its text property:

someTextView.text = @"Some Text";

Are you asking if it's "better" to use a UILabel or UITextField? That's a subjective question. It depends on how your app is designed and what makes the most sense for presenting textual information. (Presenting formatted HTML in a UIWebView is another option.)

Once you set the text of any of these built-in classes, it will know to update/redraw itself. You don't need to set text and then trigger the redrawing or "repainting" manually.

傾城如夢未必闌珊 2024-09-26 06:29:30

[myTextView setNeedsDisplay]; 有效吗?这是告诉 iOS UIView 它需要查看自身重绘的传统方法。 UIView 文档 包含一些这方面的好信息。

Does [myTextView setNeedsDisplay]; work? That is the traditional way to tell an iOS UIView that it needs to see about redrawing itself. The UIView documentation contains some good information on that.

十六岁半 2024-09-26 06:29:30

好的,我现在得到了解决方案。与往常一样,只要您知道如何操作,它就非常简单:-)
看来我对最初的问题描述不是很清楚,这里再次说明功能:
我需要我的应用程序在 UITextView 中显示 SQLITE3 数据库中的文本,并每 2 秒使用数据库中下一条记录的文本更新该文本。所以我最初的想法是在 Select 循环中完成它。有了毛茸茸的提示,视图不会更新,在完成循环之前,因为它在单个线程中运行,我开始寻找另一个解决方案,并提出了以下基于选择器的解决方案,效果非常好。

用户界面中任何对象的这种编程和迭代更新的解决方案是这样的方法

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

}

在我的例子中,当用户触摸 UIButton 时会调用该方法。要使计划停止,需要向计时器实例发送以下消息:

        [timer invalidate];

当然,我必须稍微更改一下代码才能与计划的计时器一起运行,但从迭代的角度来看,上面的代码就是您需要的一切触发并停止调度程序。希望这也能帮助其他遇到类似问题的人。

干杯,雷内

Ok I got the solution now. As always, it is pretty simple as soon as you know how :-)
As it seems, that I was not very clear on my initial problem description, here the feature description again:
I needed my app to display text from my SQLITE3 database in a UITextView and update that text every 2 seconds with the text from the next record in the DB. So my initial thought was to do it within the Select loop. With the hint of shaggy, that it the view will not be updated, before finishing the loop as it is running in a single thread, I started to look for another solution and came up with the following selector based solution, which works fantastic.

The solution to such a programatic and iterative update of any object in the user interface is a method like this

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

}

This method in my case is invoked when the user touches a UIButton. To make the schedule stop, it is required to send the following message to the timer instance:

        [timer invalidate];

Of course I had to change my code a bit to run with the scheduled timer, but from an iteration perspective, the above code is everything you need to trigger and stop a scheduler. Hope that this helps others with a similar issue too.

Cheers, René

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