2010-04-20 116 views

回答

9

不知道你的意思「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

-2

如果您要添加的圖像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

7

您可以設置文本視圖圖層的內容屬性。

#import <QuartzCore/QuartzCore.h> 

... 

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

應該工作