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?
Asked
Active
Viewed 69 times
0

Marinos K
- 1,779
- 16
- 39
-
What do you mean by preamble colour ? – Abhishek Mitra Apr 11 '17 at 08:27
-
http://stackoverflow.com/questions/33414757/how-to-change-uitexefield-placeholder-color-and-fontsize-using-swift-2-0/33415013#33415013 – Kirit Modi Apr 11 '17 at 09:21
-
yes it's probably a duplicate, sorry for that. – Marinos K Apr 11 '17 at 09:36
-
2Possible duplicate of [How to change UITexefield placeholder color and fontsize using swift 2.0?](http://stackoverflow.com/questions/33414757/how-to-change-uitexefield-placeholder-color-and-fontsize-using-swift-2-0) – Marinos K Apr 11 '17 at 09:37
2 Answers
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
-
-
for the preamble text use: textField.attributedPlaceholder = NSAttributedString(string: "preamble text", attributes:[NSForegroundColorAttributeName:UIColor.redColor()]) – Sumeet.Jain Apr 11 '17 at 09:14