在滚动文本视图之前,无法在UITextView中看到文本 [英] Can't see text in UITextView until text view is scrolled

查看:68
本文介绍了在滚动文本视图之前,无法在UITextView中看到文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一堆textview。出于某种原因,无论我是以编程方式(来自互联网)还是通过界面构建​​器(硬编码)设置UITextView文本,无论我做什么,当我在测试时转到该文本视图时它都是空白的。但是当我滚动它的所有文本只是出现在没有的地方。只是为了澄清,我并不是说textview不会滚动,因为没有足够的文本。 textview中有文本编程,但是当应用程序运行时它不显示任何文本,直到文本视图实际滚动,然后文本才会出现。

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.

代码:
h文件

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


推荐答案

我猜你的UITextView在设置它的文本时是不可见的,因此没有绘制文本。

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

一旦你的UITextView可见,你可以强迫它通过这样做来绘制文本:

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

[textView setNeedsDisplay];

或许这样:

[textView layoutSubviews];

否则,在UITextView可见之前不要设置它的文本属性。

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

其他可能性:


  1. 您的UITextView是否在包含您的UITextView的视图的框架内?如果父视图没有剪辑子视图,并且UITextView在父视图的框架之外可能是您的问题。问题还可能是父视图在父视图的父视图边界之外,或父视图的父视图的边界等等。

  2. 您是否正在尝试将大量文本加载到父视图中。文字视图?如果是这样,尝试做一些简单的事情,如textView.text = @test;并查看是否有效。

  3. 你的textView是UIScrollView或UIWebView的子视图吗?这肯定会带来问题。

  4. 你的textView是否在另一个视图后面?如果是这样,请尝试将文本视图放在前面,看看是否能解决问题。

  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.

一般来说,你有一些布局问题导致UITextView无法绘制文本。出于某种原因,它认为不需要绘制文本。也许它不认为它在屏幕上或可见。

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

这篇关于在滚动文本视图之前,无法在UITextView中看到文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆