Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView background image

How can I set up a background image to UITextView?

like image 396
Ilya Suzdalnitski Avatar asked Apr 17 '09 09:04

Ilya Suzdalnitski


5 Answers

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.

like image 162
duncanwilcox Avatar answered Nov 06 '22 06:11

duncanwilcox


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];
like image 24
oxigen Avatar answered Nov 06 '22 08:11

oxigen


@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"]];
like image 42
Zaraki Avatar answered Nov 06 '22 07:11

Zaraki


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];
like image 11
Chapin Avatar answered Nov 06 '22 06:11

Chapin


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)];
}
...
like image 6
Leonid Melnyk Avatar answered Nov 06 '22 06:11

Leonid Melnyk