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

Joe Sloan picture Joe Sloan · May 3, 2016 · Viewed 46k times · Source

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?

Answer

appiconhero.co picture appiconhero.co · May 3, 2016

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