xcode 在 TextView 后面添加背景图片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5084406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add Background Image behind TextView
提问by PatrickGamboa
Hey guys, Does anyone know how to make the Text View transparent and add a Background Image behind the Text View? Thanks
嘿伙计们,有谁知道如何使文本视图透明并在文本视图后面添加背景图像?谢谢
回答by Zaky German
In viewDidLoad
在视图中DidLoad
textView.backgroundColor = [UIColor clearColor];
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:textView.frame] autorelease];
imageView.image = [UIImage imageNamed:@"myBackgroundImage.png"];
[self.view addSubview:imageView];
[self.view bringSubviewToFront:textView];
回答by Mats Stijlaart
Try to make a new class. (Did not test the code, just typed it on the forum, try to use the code otherwise: use the concept)
尝试创建一个新类。(没有测试代码,只是在论坛上输入,否则尝试使用代码:使用概念)
@interface TextViewWithImage : UIView {
UITextView *textView;
UIImageView *imageView;
}
@property (retain, readonly) UITextView *textView;
@property (retain, readonly) UIImageView *imageView;
@implementation TextViewWithImage
- (id) init {
if (self = [super init]) {
[self setupContentViews];
}
return self;
}
- (id) initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setupContentViews];
}
return self;
}
- (void) setupContentViews {
textView = [[UITextView alloc] init];
imageView = [[UIImageView alloc] init];
textView.frame = self.frame;
imageView.frame = self.frame;
textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
- (void) dealloc {
[textView release];
[imageView release];
[super dealloc];
}
@end
回答by dkaranovich
UIViews (of which UITextView is a subclass) have a property called backgroundColor.
UIViews(其中 UITextView 是其子类)有一个名为 backgroundColor 的属性。
@property(nonatomic, copy) UIColor *backgroundColor
Try calling [textView setBackgroundColor:[UIColor clearColor]] on the TextView and putting an image view behind it.
尝试在 TextView 上调用 [textView setBackgroundColor:[UIColor clearColor]] 并将图像视图放在其后面。