3

我有一个大小为 360X44 的背景图像,我试图将其作为我的 UITextFields 的背景,但是它显示出意外的行为。光标在图像开始之前出现,但文本被写入写入位置。

背景图像

我的 UiTextField 的实现

UITextField *textFieldRounded = [[UITextField alloc] initWithFrame:CGRectMake(100, 75, 215, 30)];
textFieldRounded.borderStyle = UITextBorderStyleNone;
UIImage *fieldBGImage = [[UIImage imageNamed:@"textfield_normal.png"]  stretchableImageWithLeftCapWidth:50 topCapHeight:50];
[textFieldRounded setBackground:fieldBGImage];
textFieldRounded.textColor = [UIColor blackColor];
textFieldRounded.font = [UIFont systemFontOfSize:17.0];
textFieldRounded.placeholder = @"http://";  //place holder
textFieldRounded.backgroundColor = [UIColor whiteColor];
textFieldRounded.autocorrectionType = UITextAutocorrectionTypeNo;
textFieldRounded.backgroundColor = [UIColor clearColor];
textFieldRounded.keyboardType = UIKeyboardTypeDefault;
textFieldRounded.returnKeyType = UIReturnKeyDone;
textFieldRounded.clearsOnBeginEditing = NO;
textFieldRounded.clearButtonMode = UITextFieldViewModeWhileEditing;    
[self.view addSubview:textFieldRounded];    
textFieldRounded.delegate = self;
4

3 回答 3

4

我只是像我的波纹管代码一样设置 TextFiled 背景图像并且工作良好:-

        [YourTextFiled setFont:[UIFont systemFontOfSize:18]];
        YourTextFiled.textAlignment = UITextAlignmentLeft;
        YourTextFiled.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 12, 20)];
        YourTextFiled.leftViewMode = UITextFieldViewModeAlways;
        YourTextFiled.background = [[UIImage imageNamed:@"xFOEr.png"] stretchableImageWithLeftCapWidth:7 topCapHeight:17];

看起来像”-

在此处输入图像描述

于 2013-01-31T05:38:22.450 回答
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];

Dont use background property. Add image as subview and send subview to back.

于 2013-01-31T09:01:16.840 回答
0

我建议不要将图像添加为背景颜色。将其添加为不同的组件。即首先添加一个UIImageView然后添加你的UITextField就可以了。

同时尝试在您当前的代码中进行此更改:

UITextField *textFieldRounded = [[UITextField alloc] initWithFrame:CGRectMake(100, 75, 360, 44)];
于 2013-01-31T05:31:41.487 回答