虽然我已经在UITextField
中设置了背景图像属性,但它并没有应用。我没有使用任何编码,只是使用界面构建器选择了一个名为image.png
的文件作为背景图像。所以,如何实现呢?
而在UITextView
中没有背景图像属性。那么,如何将背景图像添加到textView
中呢?
感谢您的帮助。
虽然我已经在UITextField
中设置了背景图像属性,但它并没有应用。我没有使用任何编码,只是使用界面构建器选择了一个名为image.png
的文件作为背景图像。所以,如何实现呢?
而在UITextView
中没有背景图像属性。那么,如何将背景图像添加到textView
中呢?
感谢您的帮助。
该属性的默认值为UITextBorderStyleNone。如果将值设置为UITextBorderStyleRoundedRect样式,则忽略与文本字段关联的自定义背景图像。
在Interface Builder中,在设置背景图像的位置下方应该有一个选择边框样式的选项,默认情况下选择了RoundedRect样式,选择其他样式后,您可以立即看到背景图像。
使用这个
textField.backgroundColor = [UIColor colorWithPatternImage:myImage];
其中“myImage”是您的图像。
Swift 4
首先,将图像添加到您的Assets.xcassets
中,使用您想要的名称,例如myImage
。在您的文本字段中,您可以使用以下方式设置它:
myTextField.background = UIImage(named: "myImage")
myTextField.layer.borderColor = CGColor.clear
myTextField.layer.borderWidth = 0
您可以使用以下方式设置TextView的背景图片
[textView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage.png"]]];
对于您的文本字段,您可以使用两种方法之一来设置图像,第一种是以前的方法,另一种是
[textField setBackground:[UIImage imageNamed:@"yourImage.png"]];
CGRect frame1 = CGRectMake(30, 130,160, 30);
textField = [[UITextField alloc] initWithFrame:frame1];
textField.borderStyle =UITextBorderStyleNone;
textField.background=[UIImage imageNamed:@"textFieldImage.png"];
如果要为没有背景图像属性的视图添加背景图像,您需要进行一些技巧。您需要创建一个UIImageView,然后将UITextView作为其子视图添加到其中。同时确保UITextView的背景颜色设置为透明。例如:
UIImageView *imageView = [[UIImageView alloc] initWithimage:myImage];
[self.view addSubview:imageView];
[imageView addSubview:myTextView];
UITextField
的背景颜色设置为透明色? - sunkehappy