简体   繁体   English

UITextView背景图片

[英]UITextView background image

如何为UITextView设置背景图像?

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). 您可以将包含背景图像的UIImageViewUITextView作为兄弟,然后在Interface Builder中移动文本视图以重叠图像视图(或者如果以编程方式执行,则将它们添加到同一父视图中)。 You also need to make sure that text view is not opaque and give it a 0% opacity background . 您还需要确保文本视图不是不透明的,并为其提供0%不透明度背景

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];

@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. @durai:您的图像有一个高度限制,如果在向下滚动后出现空白背景,则会出现白色背景,然后您可以重复相同的图像。

This might be helpful: 这可能会有所帮助:

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

Just one clarification, when trying out answer #9 from @oxigen , I found out that this line: 只有一点澄清,当试用@oxigen的答案#9时,我发现这一行:

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

Is relative to the textView.frame . 是相对于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: 因此,如果您希望它完全重叠,那么您的xy值必须为0,0这意味着您需要以下内容:

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

I found one simple method how to set background image. 我找到了一个简单的方法来设置背景图像。

h file h文件

@interface FNTextView : UITextView

@end

m file m文件

...
- (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)];
}
...
[txtView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"image.png"]]];

For tiled background i like this solution 对于平铺背景我喜欢这个解决方案

UIImage *patternImage = [UIImage imageNamed:@"image.png"];
UIColor* color = [UIColor colorWithPatternImage:patternImage];
textView.backgroundColor = color;
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];

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. @dulcanwilcox和@oxigen的答案都有效,但是,如果图像用于显示某种边框,则可以增加背景图像的大小。

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 . 查看resizableImageWithCapInsets的文档:(UIEdgeInsets)capInsets

Not sure about, but you can try the following, assuming that your background image is handled by a UIImageView: 不确定,但您可以尝试以下方法,假设您的背景图像由UIImageView处理:

[myTextView addSubview: myImageView]; [myTextView addSubview:myImageView];

Note that you may need to change the value of the alpha/opaque properties of your UITextView. 请注意,您可能需要更改UITextView的alpha / opaque属性的值。

Kind Regards. 亲切的问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM