Romes
Romes

Reputation: 3118

iOS UITextView - setText method not showing my text?

I've created a UITextView programmatically with the follow:

messageBodyTxt = [[UITextView alloc] initWithFrame:CGRectMake(36, 204, 763, 251)];
[messageBodyTxt setTextColor:[UIColor colorWithRed:0.627 green:0.627 blue:0.627 alpha:1]];
[messageBodyTxt setBackgroundColor:[UIColor clearColor]];
[messageBodyTxt setText:@"some random text"];

However, the setText property is not showing my text. My header file looks like this:

#import <UIKit/UIKit.h>

@interface MessageView : UIView {
    UITextView *messageBodyTxt;
}

@end

and i've added it to my view like so:

[self addSubview:messageBodyTxt];

All of my other UILabel's function properly when I use the setText method, however I can't seem to get the UITextView to show anything.

Any ideas?

Upvotes: 2

Views: 16780

Answers (2)

Romes
Romes

Reputation: 3118

I figured it out. The frame of the parent view was too small, and not being clipped, so I could see all of my other elements in the view showing up, but they were not clickable (eg. buttons and such)

As soon as I fixed the parent view frame to be the correct size, the `UITextView text magically showed up.

Thanks everyone you'd done great!

Upvotes: 2

kaspartus
kaspartus

Reputation: 1365

It's really strange. I create "1 view" project. Then: ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController: UIViewController {
    UITextView *exampleTxt;
}

@end

Go to ViewController.m (viewDidLoad):

-(void)viewDidLoad{
    [super viewDidLoad];
    exampleTxt = [[UITextView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    [exampleTxt setText:@"Everything Ok"];
    [exampleTxt setColor:[UIColor redColor]]; //yes, it's deprecated, but works
    [self.view addSubview: exampleTxt]; //because its ViewController
}

And everything ok. Looks like troubles in link your viewController with your MessageView. Try to use Interface Builder. Or show more code.

Upvotes: 2

Related Questions