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

你。 提交于 2019-12-03 23:08:10

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

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"

Important to note for anyone else reading this, setting placeholder text in the main.storyboard seems to nullify this solution, so I had to first clear out my placeholders in the storyboard before implementing this. Once that was done @Khuong and @Himanshu's answer worked perfectly.

Apply this line of code in to View Did Load

new_Password.attributedPlaceholder =
            NSAttributedString(string: " New Password", attributes: [NSForegroundColorAttributeName : UIColor.white]) // new_Password : our text feild name
Himanshu

Fetch your desired data from your database (Core data) and after converting it into string format... say phoneString

use this line to set this string as a placeholder text

phoneTextField.placeholder = phoneString

Objective-C code:

[usernameText setPlaceholder:@"My Placeholder Text"];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!