Cursor appears before setting the background image of the UITextField
I have a 360X44 background image that I am trying to place as the background of my UITextFields, however it exhibits unexpected behavior. The cursor appears before the start of the picture, but texts are written to the recording location. Please help me.
Implementation of my 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;
source to share
I just set the background TextFiled like my following code AND it works Good: -
[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];
looks like "-
source to share
I would suggest instead of adding an image as a background. Add it as another component. Add first UIImageView
and then add to it UITextField
.
In the meantime, try making this change in your current code:
UITextField *textFieldRounded = [[UITextField alloc] initWithFrame:CGRectMake(100, 75, 360, 44)];
source to share
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];
Don't use the background property. Add the image as a sub-request and send the sub-request back.
source to share