Reputation: 516
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?
Upvotes: 2
Views: 2182
Reputation: 13577
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;
}
Upvotes: 1
Reputation: 819
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".
Upvotes: 1
Reputation: 41
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.
Upvotes: 0
Reputation: 871
I have found some similar custom textfield in the below URL. If you find this suitable, you can use it.
Upvotes: 2
Reputation: 244
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;
Upvotes: 1