I need to keep the placeholder after the text, when click on the text box i need to clear only the placeholder and able to type characters.
In this Marissa is the name text and (First Name) is placeholder. Once i start editing placeholder need to clear and start editing complete text.
How can i achieve that?
I have found some similar custom textfield in the below URL. If you find this suitable, you can use it.
Step 1: Make the text filed attributed form xib
and set the "Marisa (First Name)" and change the color for the (First Name) as grey color .
Step 2: And in the Textfiled DidBiginEditing
method change the textField.text
to "Marisa".
Please refer below code.
Just copy-paste below code in viewdidload()
textfield.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"Yourtext%@",Placehodertext] attributes:@{NSForegroundColorAttributeName: color}];
Assign Delegate of UITextfield
to self
.
textfield.delegate = self
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
textfield.text = @"Yourtext"
return YES;
}
You can not set place holder along with the text in a textfield. But there is one alternate for this. Textfields has something called left view which you can make readOnly view. Try this :
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
UILabel *textLabel = [[UILabel alloc] initWithFrame:paddingView.bounds];
textLabel.text = @"Marissa";
[paddingView addSubView:textLabel];
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;
This could be achieved in several ways:
Use the attributedText
property of UITextField
instance to apply a styling for different parts of the text. Here you need to properly define the property each time a user has changed the input by using delegate methods or by observing UIControl
Control Events .
If the structure of the text input is fixed and predefined you could use a batch of fields to imitate a more complex/smart field. Here, for example, you need to put two fields beside. One for the last name, second for the first name. Or you can use a combination of UILabel
and UITextField
if one the part is not editable. Such an approach allows you to configure a separate part of a complex field as you want: different placeholders, fonts, keyboard styles, etc.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.