-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.

6
  • 1
    Check this question: stackoverflow.com/questions/26076054/… Commented Sep 19, 2018 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 - stackoverflow.com/questions/1340224/… Commented Sep 19, 2018 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. Commented Sep 19, 2018 at 21:19
  • 1
    @JamesC you say that you wouldn't have figured to change UIColor.white to UIColor.black from this answer stackoverflow.com/a/26076202/1974224 ? Commented Sep 19, 2018 at 21:30
  • 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. Commented Sep 19, 2018 at 21:45

2 Answers 2

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 }() 
Sign up to request clarification or add additional context in comments.

Comments

-4

You can try this (Swift 4.1)

emailTF.attributedPlaceholder = NSAttributedString(string:"Enter Email", attributes: [NSAttributedStringKey.foregroundColor:UIColor.white,NSAttributedStringKey.backgroundColor:UIColor.black]) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.