UITextView在滚动之前不会显示

时间:2021-05-29 21:13:58

I am using a UITextView to display the text from a xml.I parsed the xml and stored the text in a NSArray and from there i am adding it to a UITextview in my code.

我正在使用UITextView来显示xml中的文本。我解析了xml并将文本存储在NSArray中,然后我将其添加到我的代码中的UITextview中。

problem: When i run the code the textview shows as empty or with partial text,and when i try to scroll the textview the whole text is getting displayed.

问题:当我运行代码时,textview显示为空或部分文本,当我尝试滚动textview时,整个文本显示。

I added the UITextview to a UIView.

我将UITextview添加到UIView中。

i found it strange and googled it but could not find many answers which helps me. Can someone reply me to solve the issue

我发现它很奇怪,谷歌搜索它,但找不到很多帮助我的答案。有人可以回复我解决问题

TNQ

TNQ

8 个解决方案

#1


10  

Found a simple SOLUTION

找到了简单的解决方案

Had the same problem. When you put text into a UITextView which is not visible, it doesn't draw the text.

有同样的问题。当您将文本放入不可见的UITextView时,它不会绘制文本。

I had my UITextView off screen and animated it in like this:

我把我的UITextView关闭屏幕并像这样动画:

    [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationCurveEaseOut animations:^{
    self.contentView.center = CGPointMake(self.contentView.center.x + move * (self.contentView.frame.size.width /2), self.contentView.center.y);
    self.textView.frame = self.textView.frame; // <<<--- ADD THIS LINE
    } completion:^(BOOL finished) {
    if (finished) {
    }
}];

When you re-set the textview's frame, it re-draws it's text. So all you need to do is add the line I marked above before your UITextView comes on screen.

当您重新设置textview的框架时,它会重新绘制它的文本。因此,您需要做的就是在UITextView进入屏幕之前添加我在上面标记的行。

Cheers

干杯

#2


5  

UITextView inherits from UIScrollView, so just scroll it.

UITextView继承自UIScrollView,因此只需滚动它。

[detailsTextView setContentOffset:CGPointMake(0, 1) animated:YES];

#3


1  

i solved the issue by clearing the textview and added the text to the textview again,i dont know the exact funda but my issue is solved. in my case: i am changing the frame of the textview from outside to the view on click of a button.while the button is clicked the textview is not showing content and on scroll it shows.

我通过清除textview并将文本再次添加到textview解决了这个问题,我不知道确切的基础,但我的问题已经解决了。在我的情况下:我正在点击按钮将文本视图的框架从外部更改为视图。然后单击按钮,textview不显示内容,并在滚动显示。

i solved, by deleting the text from textview and re entering the text on clicking the button.

通过从textview中删除文本并在单击按钮时重新输入文本,我解决了问题。

#4


0  

UItextView inherits from UIScrollView. That means it has scrolling functionality. Thus, if the contents of the UITextView is small enough to fit in its frame, it doesn't need to scroll.

UItextView继承自UIScrollView。这意味着它具有滚动功能。因此,如果UITextView的内容足够小以适合其框架,则不需要滚动。

But, if it is large enough, you need to scroll the UITextView.

但是,如果它足够大,则需要滚动UITextView。

Does this makes sense?

这有道理吗?

For more information, refer the class reference of UITextView

有关更多信息,请参阅UITextView的类参考

#5


0  

Ensure that, method in which u set textview's text is call properly.And string which u assign to text view is in proper string format(if not so retain it).

确保你设置textview文本的方法被正确调用。你分配给文本视图的字符串是正确的字符串格式(如果不是这样保留它)。

#6


0  

There are chances that the whitespaces and/or newlines are there before the actual text. Trim those characters before you assign it to text view.

在实际文本之前有可能存在空格和/或换行符。在将这些字符分配给文本视图之前修剪它们。

NSCharacterSet *charset = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSString *text = [parsedString stringByTrimmingCharactersInSet:charset];
textview.text = text;

#7


0  

Please try it when your textView is loading

请在加载textView时尝试使用它

NSAttributedString *oldtext = objTextView.attributedText;
[objTextView removeFromSuperview];
objTextView.attributedText = oldtext;
[self.view addSubview:objTextView];

In my case my text is attributed

在我的情况下,我的文字被归因

#8


0  

When you complete the operation which parses the XML are you ensuring that your update is occuring on the main thread ?

当您完成解析XML的操作时,您是否确保在主线程上发生了更新?

UIKit only updates on the main thread so any updates made off it will only show up next time you touch the object and force an update.

UIKit仅在主线程上进行更新,因此所做的任何更新只会在您下次触摸对象并强制更新时显示。

As an example of one way to do it correctly.

作为一种正确的方法的例子。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

   [self parseXMLOffMainThread];

   dispatch_async(dispatch_get_main_queue(), ^{

      self.myTextView.text = parsedText;

      });        

 });

#1


10  

Found a simple SOLUTION

找到了简单的解决方案

Had the same problem. When you put text into a UITextView which is not visible, it doesn't draw the text.

有同样的问题。当您将文本放入不可见的UITextView时,它不会绘制文本。

I had my UITextView off screen and animated it in like this:

我把我的UITextView关闭屏幕并像这样动画:

    [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationCurveEaseOut animations:^{
    self.contentView.center = CGPointMake(self.contentView.center.x + move * (self.contentView.frame.size.width /2), self.contentView.center.y);
    self.textView.frame = self.textView.frame; // <<<--- ADD THIS LINE
    } completion:^(BOOL finished) {
    if (finished) {
    }
}];

When you re-set the textview's frame, it re-draws it's text. So all you need to do is add the line I marked above before your UITextView comes on screen.

当您重新设置textview的框架时,它会重新绘制它的文本。因此,您需要做的就是在UITextView进入屏幕之前添加我在上面标记的行。

Cheers

干杯

#2


5  

UITextView inherits from UIScrollView, so just scroll it.

UITextView继承自UIScrollView,因此只需滚动它。

[detailsTextView setContentOffset:CGPointMake(0, 1) animated:YES];

#3


1  

i solved the issue by clearing the textview and added the text to the textview again,i dont know the exact funda but my issue is solved. in my case: i am changing the frame of the textview from outside to the view on click of a button.while the button is clicked the textview is not showing content and on scroll it shows.

我通过清除textview并将文本再次添加到textview解决了这个问题,我不知道确切的基础,但我的问题已经解决了。在我的情况下:我正在点击按钮将文本视图的框架从外部更改为视图。然后单击按钮,textview不显示内容,并在滚动显示。

i solved, by deleting the text from textview and re entering the text on clicking the button.

通过从textview中删除文本并在单击按钮时重新输入文本,我解决了问题。

#4


0  

UItextView inherits from UIScrollView. That means it has scrolling functionality. Thus, if the contents of the UITextView is small enough to fit in its frame, it doesn't need to scroll.

UItextView继承自UIScrollView。这意味着它具有滚动功能。因此,如果UITextView的内容足够小以适合其框架,则不需要滚动。

But, if it is large enough, you need to scroll the UITextView.

但是,如果它足够大,则需要滚动UITextView。

Does this makes sense?

这有道理吗?

For more information, refer the class reference of UITextView

有关更多信息,请参阅UITextView的类参考

#5


0  

Ensure that, method in which u set textview's text is call properly.And string which u assign to text view is in proper string format(if not so retain it).

确保你设置textview文本的方法被正确调用。你分配给文本视图的字符串是正确的字符串格式(如果不是这样保留它)。

#6


0  

There are chances that the whitespaces and/or newlines are there before the actual text. Trim those characters before you assign it to text view.

在实际文本之前有可能存在空格和/或换行符。在将这些字符分配给文本视图之前修剪它们。

NSCharacterSet *charset = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSString *text = [parsedString stringByTrimmingCharactersInSet:charset];
textview.text = text;

#7


0  

Please try it when your textView is loading

请在加载textView时尝试使用它

NSAttributedString *oldtext = objTextView.attributedText;
[objTextView removeFromSuperview];
objTextView.attributedText = oldtext;
[self.view addSubview:objTextView];

In my case my text is attributed

在我的情况下,我的文字被归因

#8


0  

When you complete the operation which parses the XML are you ensuring that your update is occuring on the main thread ?

当您完成解析XML的操作时,您是否确保在主线程上发生了更新?

UIKit only updates on the main thread so any updates made off it will only show up next time you touch the object and force an update.

UIKit仅在主线程上进行更新,因此所做的任何更新只会在您下次触摸对象并强制更新时显示。

As an example of one way to do it correctly.

作为一种正确的方法的例子。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

   [self parseXMLOffMainThread];

   dispatch_async(dispatch_get_main_queue(), ^{

      self.myTextView.text = parsedText;

      });        

 });