Daniel Kivatinos
Daniel Kivatinos

Reputation: 25006

How do I put a background image on a text area (IN THE IPAD)

How do I put a template'd background image on a text area?

Upvotes: 1

Views: 4506

Answers (3)

thelaws
thelaws

Reputation: 8001

If you want to add the image programmaticaly I managed to do it this way:

First in my textviewViewController.h I added an outlet for the UITextView.

#import <UIKit/UIKit.h>

@interface textviewViewController : UIViewController {
UITextView *textView;
}
@property (nonatomic, retain) IBOutlet UITextView *textView;

@end

Next, in my .xib I added my UITextView and connected my outlet.

Lastly in the viewDidLoad of textviewViewController.m I add the image as a subview to my textview.

- (void)viewDidLoad {
    [super viewDidLoad];
    // Change the pathForResource to reflect the name and type of your image file
    NSString *path = [[NSBundle mainBundle] pathForResource:@"d" ofType:@"jpg"];
    UIImage *img = [UIImage imageWithContentsOfFile:path];

    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
    [self.textView insertSubview:imgView atIndex:0];
    [imgView release];

}

I tried the insertSubview:atIndex: with both 0 and 1 with the same result, but I would stick with 0 idicating that it's behind the textview.

Upvotes: -2

Elfred
Elfred

Reputation: 3851

You can set the contents property of the text views layer.

#import <QuartzCore/QuartzCore.h>

...

- (void) viewDidLoad {
    [super viewDidLoad];
    textView.layer.contents = (id)[UIImage imageNamed:@"myImage.png"].CGImage
}

should work

Upvotes: 7

wkw
wkw

Reputation: 3863

Not sure what you mean with "template'd"... but....

In interface builder, add the imageview behind the textview, uncheck the "opaque" box for the textview.

Upvotes: 9

Related Questions