Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text in UITextView not display correctly, invisible, but occupied spaces

There is a UITextView in the view, the view controller is showing with Modal style. In my viewDidLoad method, I set the text of this UITextView, but, the text is not showing. Image below showing the error. Text color is black.

The weird thing is , when I long tap in the text view or tap [return] in keyboard, the text become visible. One thing I noticed is this error only occurred when the text I set is longer than the UITextView frame width, and the last word is not broken such as a long url.

I think the problem is maybe the word wrap not work correctly.

Thanks in advance.

enter image description here

Code like below:

UITextView *myTextView = [[UITextView alloc]initWithFrame:CGRectMake(10, 10, 520, 220)];
myTextView.textColor = [UIColor blackColor];    
myTextView.font = [UIFont systemFontOfSize:20];
myTextView.layer.cornerRadius = 5;
myTextView.delegate = self;
myTextView.text = @"long text should some like http://stackoverflow.com/posts/11200726/edit";

[self.view addSubview:myTextView];
[myTextView release];

RESOVLED. In viewDidLoad method, add code below:

CGRect tempFrame = myTextView.frame;
[myTextView setFrame:CGRectZero];
[myTextView setFrame:tempFrame];
like image 208
Wubao Li Avatar asked Jun 26 '12 04:06

Wubao Li


1 Answers

Subclass UITextView, to a class of your own, let's say MyUITextView:UITextView.

Initialize it offscreen with

[[MyUITextView alloc] initWithFrame:CGRectZero]

In MyUITextView override the method

-(void)willMoveToWindow:(UIWindow *)newWindow

and in it, set self.frame to the proper value.

this worked for me!

like image 64
gabouy Avatar answered Nov 16 '22 19:11

gabouy