开发者

UITextView doesn't show until it is scrolled

开发者 https://www.devze.com 2023-04-12 16:54 出处:网络
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.

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.

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.

I added the UITextview to a 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


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.

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

    [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.

Cheers


UITextView inherits from UIScrollView, so just scroll it.

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


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.

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


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.

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

Does this makes sense?

For more information, refer the class reference of UITextView


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).


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;


Please try it when your textView is loading

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

In my case my text is attributed


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

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.

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;

      });        

 });
0

精彩评论

暂无评论...
验证码 换一张
取 消