如何在文本区域放置背景图像(在IPAD中)

问题描述:

如何在文本区域放置模板的背景图像?如何在文本区域放置背景图像(在IPAD中)

不知道你的意思“template'd”什么......但....

在Interface Builder中,添加ImageView的TextView的背后,取消选中“不透明”框TextView的。

+0

尝试这个现在WKW!如果它有效,你的答案就会赢! – 2010-05-25 20:06:49

+0

你如何把textview放在textview后面? – 2010-05-26 06:24:19

+1

您只需放入图像视图,您可能必须更改textview的z顺序,使其位于图像视图之上。 – Ukko 2010-05-27 18:33:16

如果您要添加的图像programmaticaly我设法做这种方式:

首先在我textviewViewController.h我增加了一个出口的UITextView的。

#import <UIKit/UIKit.h> 

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

@end 

接下来,在我的.xib中,我添加了我的UITextView并连接了我的插座。

最后在textviewViewController.m的viewDidLoad中,我将该图像作为子视图添加到我的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]; 

} 

我试过insertSubview:atIndex:既0和1具有相同的结果,但我会用0 idicating它的TextView的背后贴。

+1

'[self.textView insertSubview:imgView atIndex:0];'将图像插入到textView中,而不是在后面。使用'[[self.textView superview] insertSubview:imgViewSubview:self.textView];'把它放在textView后面,假设superview不是零。后者不会随文本一起滚动。插入后不要忘记释放imgView。 – drawnonward 2010-05-28 03:36:59

+0

感谢drawnonward,你是对的我忘了发布我的imgView。由于这是在我的视图控制器中,imageView可以通过[self.view insertSubview:]插入到主视图中,但是我得到了imageView被视为textview一部分的印象。 – thelaws 2010-06-02 02:03:03

您可以设置文本视图图层的内容属性。

#import <QuartzCore/QuartzCore.h> 

... 

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

应该工作