如何将背景图像放到iPhone的文本字段和文本视图中?

时间:2012-12-03 14:49:57

标签: iphone ios xcode uitextfield uitextview

虽然我在UITextField中设置了背景图片属性。它不适用。我没有使用任何编码。我刚刚使用Interface Builder选择了image.png文件作为背景图像。那么,怎么做呢? 并且UITextView中没有背景图片属性。那么,如何将背景图像添加到textView? 谢谢你的帮助。

6 个答案:

答案 0 :(得分:17)

请参阅UITextField的文档,您将找到以下内容:

此属性的默认值为UITextBorderStyleNone。如果该值设置为UITextBorderStyleRoundedRect样式,则忽略与文本字段关联的自定义背景图像。

在Interface Builder中,在您设置背景图像的位置下方,应该有一个选项来选择边框样式,默认情况下它选择为RoundedRect,选择另一种样式,您可以立即看到背景图像。 / p>

答案 1 :(得分:5)

使用此

textField.backgroundColor = [UIColor colorWithPatternImage:myImage];

其中“myImage”是你的形象。

答案 2 :(得分:1)

快捷键4

第一个,使用您想要的名称将图像添加到Assets.xcassets,例如myImage。在您的文本字段中,可以使用以下命令进行设置:

myTextField.background = UIImage(named: "myImage")

此外,如果要删除边框:

myTextField.layer.borderColor = CGColor.clear
myTextField.layer.borderWidth = 0

答案 3 :(得分:0)

您可以使用

设置TextView backGround图像
[textView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage.png"]]];

对于yourTextField,您可以使用2方法设置图像,先是先前,其他是

[textField setBackground:[UIImage imageNamed:@"yourImage.png"]];  

答案 4 :(得分:0)

 CGRect frame1 = CGRectMake(30, 130,160, 30);
textField = [[UITextField alloc] initWithFrame:frame1];
textField.borderStyle =UITextBorderStyleNone;
textField.background=[UIImage imageNamed:@"textFieldImage.png"];

答案 5 :(得分:-2)

要向没有背景图像属性的视图添加背景图像,您需要做一些技巧。您需要创建一个UIImageView,然后将UITextView添加为子视图。确保UITextView背景颜色也设置为清除。例如:

UIImageView *imageView = [[UIImageView alloc] initWithimage:myImage];
[self.view addSubview:imageView];
[imageView addSubview:myTextView];
相关问题