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

问题描述 投票:3回答:6

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

iphone ios xcode uitextfield uitextview
6个回答
17
投票

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

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

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


5
投票

用这个

textField.backgroundColor = [UIColor colorWithPatternImage:myImage];

其中“myImage”是你的形象。


1
投票

斯威夫特4

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

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

此外,如果要删除边框:

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

0
投票

您可以使用设置TextView backGround图像

[textView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage.png"]]];

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

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

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

-2
投票

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

UIImageView *imageView = [[UIImageView alloc] initWithimage:myImage];
[self.view addSubview:imageView];
[imageView addSubview:myTextView];
© www.soinside.com 2019 - 2024. All rights reserved.