0

UITextField's .textColor changes both the colour of the initial preamble text and the colour of any subsequent text the user may input. I want to have the preamble colour red and any subsequent text black - is that possible?

Marinos K
  • 1,779
  • 16
  • 39

2 Answers2

1

You can't set the colour of the placeholder text in Interface Builder, but if you set the text in Interface Builder, then you can "colorise" it using something like

self.textField.attributedPlaceholder = NSAttributedString(
 string: self.textField.placeholder!, 
 attributes: [NSForegroundColorAttributeName:UIColor.green])

If you aren't using Interface Builder then you can just specify the desired string directly

self.textField.attributedPlaceholder = NSAttributedString(
 string: "My placeholder text", 
 attributes: [NSForegroundColorAttributeName:UIColor.green])
Paulw11
  • 108,386
  • 14
  • 159
  • 186
0

use placeholder for preamble text and can provide textcolor to placeholder using textField.attributedPlaceholder

now you can have two colors for the textField.

Sumeet.Jain
  • 1,533
  • 9
  • 26
  • not sure I follow you, any code or links on how to do this? – Marinos K Apr 11 '17 at 08:35
  • for the preamble text use: textField.attributedPlaceholder = NSAttributedString(string: "preamble text", attributes:[NSForegroundColorAttributeName:UIColor.redColor()]) – Sumeet.Jain Apr 11 '17 at 09:14