Ilya Suzdalnitski
Ilya Suzdalnitski

Reputation: 53540

UITextView background image

How can I set up a background image to UITextView?

Upvotes: 36

Views: 41375

Answers (10)

Mateus
Mateus

Reputation: 2668

The answers of @dulcanwilcox and @oxigen both work, but, an addition would be to make the background image resizable, in case the image is being used to show some kind of border.

UITextView *textView = [[UITextView alloc]initWithFrame: window.frame];
textView.text = @"Some text...";

UIImageView *imgView = [[UIImageView alloc]initWithFrame: textView.frame];
imgView.image = [[UIImage imageNamed: @"image"] resizableImageWithCapInsets:UIEdgeInsetsMake(8, 8, 8, 8)];

[textView addSubview:imgView];
[textView sendSubviewToBack:imgView];

[window addSubview:textView];

Check out the documentation for resizableImageWithCapInsets:(UIEdgeInsets)capInsets.

Upvotes: 1

Chapin
Chapin

Reputation: 193

Just one clarification, when trying out answer #9 from @oxigen, I found out that this line:

UIImageView *imgView = [[UIImageView alloc]initWithFrame: textView.frame];

Is relative to the textView.frame. So your x and y values need to be 0,0 if you want it to overlap completely which means you want something like:

UIImageView *imgView = [[UIImageView alloc]initWithFrame:textView.bounds];

Upvotes: 11

Zaraki
Zaraki

Reputation: 3730

@durai: Your image has a height limit after which white background will appear if empty background appears after it scrolls down then you can repeat the same image.

This might be helpful:

textView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed: @"Image.png"]];

Upvotes: 42

duncanwilcox
duncanwilcox

Reputation: 3498

You can have an UIImageView containing the background image and the UITextView as siblings, then in Interface Builder move the text view to overlap the image view (or add them both to the same parent view if doing it programmatically). You also need to make sure that text view is not opaque and give it a 0% opacity background.

Upvotes: 71

Muhammad Asad
Muhammad Asad

Reputation: 1013

[txtView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"image.png"]]];

Upvotes: 5

oxigen
oxigen

Reputation: 6263

UITextView *textView = [[UITextView alloc]initWithFrame: window.frame];
textView.text = @"text\n text\n text";
UIImageView *imgView = [[UIImageView alloc]initWithFrame: textView.frame];
imgView.image = [UIImage imageNamed: @"myImage.jpg"];
[textView addSubview: imgView];
[textView sendSubviewToBack: imgView];
[window addSubview: textView];

Upvotes: 45

Leonid Melnyk
Leonid Melnyk

Reputation: 121

I found one simple method how to set background image.

h file

@interface FNTextView : UITextView

@end

m file

...
- (void)drawRect:(CGRect)rect
{
    [self.bgImage drawInRect:rect];

    [super drawRect:rect];
}

- (void)initHandler
{
    self.bgImage = [[UIImage imageNamed:@"textview_bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(4, 4, 4, 4)];
}
...

Upvotes: 6

Arash Zeinoddini
Arash Zeinoddini

Reputation: 819

UITextView *textView=[[UITextView alloc]initWithFrame: CGRectMake(20, 20, 40, 40)];
textView.text = @"this is a test \n this is test \n this is a test";
UIImageView *img = [[UIImageView alloc]initWithFrame: textView.frame];
img.image = [UIImage imageNamed: @"image.png"];
[self.view addSubview:textView];
[self.view insertSubview:img belowSubview:textView];

Upvotes: 1

Remizorrr
Remizorrr

Reputation: 2322

For tiled background i like this solution

UIImage *patternImage = [UIImage imageNamed:@"image.png"];
UIColor* color = [UIColor colorWithPatternImage:patternImage];
textView.backgroundColor = color;

Upvotes: 2

Massimo Cafaro
Massimo Cafaro

Reputation: 25419

Not sure about, but you can try the following, assuming that your background image is handled by a UIImageView:

[myTextView addSubview: myImageView];

Note that you may need to change the value of the alpha/opaque properties of your UITextView.

Kind Regards.

Upvotes: 1

Related Questions