-2

Im trying to change the placeholder text in textfield to white with a background color of black. Programmatically

Here is my code

let emailTextField: UITextField = {
    let emailTF = UITextField()
    emailTF.attributedPlaceholder = NSAttributedString(string: "Enter Email", attributes: [kCTForegroundColorAttributeName as NSAttributedStringKey: kCISamplerWrapBlack])

    emailTF.translatesAutoresizingMaskIntoConstraints = false
    //User input text white
    emailTF.textColor = UIColor.white
    emailTF.backgroundColor = UIColor.black
    emailTF.borderStyle = .roundedRect
    emailTF.font = UIFont.systemFont(ofSize: 16)
    return emailTF
}()

Thank you in advance.

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
James C
  • 9
  • 3
  • 1
    Check this question: https://stackoverflow.com/questions/26076054/changing-placeholder-text-color-with-swift – Ashik Sep 19 '18 at 20:38
  • 2
    Welcome to Stack Overflow.This question has been asked and answered many times before. Please do some at least a simple search before asking - https://stackoverflow.com/questions/1340224/iphone-uitextfield-change-placeholder-text-color#13695462 – Ashley Mills Sep 19 '18 at 20:51
  • Thank you. Unfortunately you may be a little quick to judge others. Neither post has the answer SH_Khan provided. The missing KEY - NSAttributedStringKey.backgroundColor:UIColor.black. – James C Sep 19 '18 at 21:19
  • 1
    @JamesC you say that you wouldn't have figured to change `UIColor.white` to `UIColor.black` from this answer https://stackoverflow.com/a/26076202/1974224 ? – Cristik Sep 19 '18 at 21:30
  • @Cristik Are you implying the code above is correct and all I needed to do was change the UIColor.white to UIColor.black. The problem was that I asked the question incorrectly. Sh_Khan was able to see passed my error and help me. Please show me where on any of the link post is the answer Sh_Khan provided. – James C Sep 19 '18 at 21:44
  • 1
    @JamesC if the linked questions didn't help you, then you should've make this clear in the question by telling us where you got stuck. As it looks now, the question is just another question that was asked N times here on SO. – Cristik Sep 19 '18 at 21:45

2 Answers2

0

Try this:

let emailTextField: UITextField = {
        let emailTF = UITextField()
        emailTF.attributedPlaceholder = NSAttributedString(string: "Enter Email",
                                                            attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])

        emailTF.translatesAutoresizingMaskIntoConstraints = false
        //User input text white
        emailTF.textColor = UIColor.white
        emailTF.backgroundColor = UIColor.black
        emailTF.borderStyle = .roundedRect
        emailTF.font = UIFont.systemFont(ofSize: 16)
        return emailTF
  }()
DiegoQ
  • 1,114
  • 11
  • 20
-4

You can try this (Swift 4.1)

emailTF.attributedPlaceholder = NSAttributedString(string:"Enter Email", attributes: [NSAttributedStringKey.foregroundColor:UIColor.white,NSAttributedStringKey.backgroundColor:UIColor.black])
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87