ios 如何将背景图像放入 iphone 中的文本字段和文本视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13685680/
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
How to put background image to a text field and text view in iphone?
提问by harshiYL
Although I have set the background image property in UITextField
. It doesn't apply.I didn't use any coding. I have just selected an image.png
file as the backround image using Interface Builder. So,How to do it ?
And there is no background image property in UITextView
. So,How can I add background image to textView
?
Thanks for the help.
虽然我已经在UITextField
. 它不适用。我没有使用任何编码。我刚刚image.png
使用 Interface Builder选择了一个文件作为背景图像。那么,怎么做呢?中没有背景图像属性UITextView
。那么,如何将背景图像添加到textView
?谢谢您的帮助。
回答by Srikanth
Please refer to the documentation of UITextField and you will find the following:
请参考 UITextField 的文档,你会发现以下内容:
The default value for this property is UITextBorderStyleNone. If the value is set to the UITextBorderStyleRoundedRect style, the custom background image associated with the text field is ignored.
此属性的默认值为 UITextBorderStyleNone。如果该值设置为 UITextBorderStyleRoundedRect 样式,则与文本字段关联的自定义背景图像将被忽略。
In Interface Builder, just below the place where you set the background image, there should be an option to select a border style, by default it is selected to RoundedRect, select another style and you can immediately see the background image.
在Interface Builder中,在你设置背景图片的地方下方,应该有一个选择边框样式的选项,默认选择的是RoundedRect,选择另一种样式可以立即看到背景图片。
回答by Yurec Antidipresant
Use this
用这个
textField.backgroundColor = [UIColor colorWithPatternImage:myImage];
where "myImage" is your image.
其中“myImage”是您的形象。
回答by MrMins
Swift 4
斯威夫特 4
First one, add the image to your Assets.xcassets
with the name you want, like myImage
. In your textfield, you can set it with:
首先,将图像添加到您Assets.xcassets
想要的名称中,例如myImage
. 在您的文本字段中,您可以设置它:
myTextField.background = UIImage(named: "myImage")
Also, if you want to remove the borders:
另外,如果要删除边框:
myTextField.layer.borderColor = CGColor.clear
myTextField.layer.borderWidth = 0
回答by Rajneesh071
You can set TextView backGround image using
您可以使用设置 TextView 背景图像
[textView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage.png"]]];
And for yourTextField you can set image using 2 method first is previous and other is
对于 yourTextField,您可以使用 2 种方法设置图像,第一种是前一种,另一种是
[textField setBackground:[UIImage imageNamed:@"yourImage.png"]];
回答by Nikunj
CGRect frame1 = CGRectMake(30, 130,160, 30);
textField = [[UITextField alloc] initWithFrame:frame1];
textField.borderStyle =UITextBorderStyleNone;
textField.background=[UIImage imageNamed:@"textFieldImage.png"];
回答by Kris Gellci
To add a background image to views that have no background image property, you will need to do some trickery. You will need to create a UIImageView and then add the UITextView as a subview. Make sure the UITextView background color is set to clear as well. For example:
要将背景图像添加到没有背景图像属性的视图中,您需要做一些技巧。您需要创建一个 UIImageView,然后将 UITextView 添加为子视图。确保 UITextView 背景颜色也设置为清除。例如:
UIImageView *imageView = [[UIImageView alloc] initWithimage:myImage];
[self.view addSubview:imageView];
[imageView addSubview:myTextView];