Can't see text in UITextView until text view is scrolled

做~自己de王妃 提交于 2019-12-22 10:58:18

问题


I have a bunch of textviews within my app. For some reason, whether I set the UITextView text programmatically (coming from the internet) or by interface builder (hardcoded), no matter what I do, when I go to that text view when testing it is blank. But the instant I scroll it all the text just appears out of no where. Just to clarify, I am not saying that the textview won't scroll because there isn't enough text. The textview has text programmed into it, but it will not display any text when the app is running until the textview is actually scrolled, then the text just appears.

Anyone know how to fix this? I've tried unlinking the view and relinking and everything.

Code: h file

@interface AboutViewController : UIViewController 
{

    IBOutlet UITextView *textView;
    NSMutableData *responseData;
    NSString *res;
}
@property(nonatomic,retain)IBOutlet UITextView *textView;
@property (nonatomic,retain)NSMutableData *responseData;

@end

m file

#import "AboutViewController.h"
@interface AboutViewController ()

@end

@implementation AboutViewController
@synthesize responseData= _responseData;
@synthesize textView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.someurl.com"]];


        [NSURLConnection connectionWithRequest:req delegate:self];
    }
    return self;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    if (!_responseData) {
        [self setResponseData:[NSMutableData data]];
    }

    [_responseData appendData:data];
}


  - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)URLresponse {

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)URLresponse;

    if (![httpResponse isKindOfClass:[NSHTTPURLResponse class]]) {
        NSLog(@"Unknown response type: %@", URLresponse);
        return;
    }


}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {

    res=[[NSString alloc] initWithData:_responseData encoding:NSUTF8StringEncoding];

    textView.text = res;

    NSLog(@"RES:%@",res);
}

- (void)viewDidLoad
{
    [super viewDidLoad];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void)dealloc{
    [super dealloc];
    [res release];
}
@end

回答1:


I am guessing that your UITextView is not visible when you are setting it's text and so the text is not being drawn.

Once your UITextView is visible, you can force it to draw the text by doing:

[textView setNeedsDisplay];

Or maybe this:

[textView layoutSubviews];

Otherwise, don't set the text property of the UITextView until it is already visible.

Other possibilities:

  1. Is your UITextView within the frame of the view containing your UITextView? If the parent view does not clip subviews and the UITextView is outside of the parent view's frame that could be your problem. The problem could also be that the parent view is outside of the parent's parent view's bounds, or the parent's parent's parent view's bounds, etc.
  2. Are you trying to load a lot of text into the text view? If so, try doing something simple like textView.text = @"test"; and see if that works.
  3. Is your textView a subview of a UIScrollView or a UIWebView? This could certainly present issues.
  4. Is your textView behind another view? If so, try placing the text view out front and see if that fixes the problem.

In general, you have some layout issue that is resulting in the UITextView not drawing it's text. For some reason it doesn't think that it NEEDS to draw the text. Maybe it doesn't think it's on screen or visible.

Alas, the most desperate of solutions could be to simulate a scroll:

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



回答2:


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



来源:https://stackoverflow.com/questions/11147621/cant-see-text-in-uitextview-until-text-view-is-scrolled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!