Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Placeholder to UITextField, how to set the placeholder text programmatically in swift?

I'm pulling out a phone number from a database, and when the user begins editing in the text field to change that phone number I'd like to use the number I currently have in the database as the placeholder. Since this information changes with each user, how can I set it programmatically in swift?

like image 855
Joe Sloan Avatar asked May 03 '16 03:05

Joe Sloan


People also ask

How do you add a placeholder in text view?

Solution #1 - If you want the placeholder to disappear as soon as the user selects the text view: First set the UITextView to contain the placeholder text and set it to a light gray color to mimic the look of a UITextField 's placeholder text. Either do so in the viewDidLoad or upon the text view's creation.

How do I change the placeholder color in UITextField in IOS Swift?

Create UITextField Extension like this: extension UITextField{ @IBInspectable var placeHolderColor: UIColor? { get { return self. placeHolderColor } set { self. attributedPlaceholder = NSAttributedString(string:self.

What is placeholder in Swift?

Type placeholders is a new language feature introduced in Swift 5.6 (Xcode 13.3). The concept is straightforward. Type placeholders allow us to write types with type placeholders ( _ ) which directs the compiler to infer the type where _ is used.

What is placeholder in Xcode?

The string that displays when there is no other text in the text field.


2 Answers

You need to get the phone number from your database first (convert them to String), then you set placeholder of your textField to that String, like so

 textField.placeholder = phoneNumberString 
like image 89
Khuong Avatar answered Sep 29 '22 11:09

Khuong


Swift 3

If your textField has text, you need to first set text property to nil, then set placeholder text:

textField.text = nil textField.placeholder = "My Placeholder Text" 
like image 25
Marcos Reboucas Avatar answered Sep 29 '22 10:09

Marcos Reboucas