This property is nil by default. If set, the placeholder string is
drawn using a 70% grey color and the remaining style information
(except the text color) of the attributed string. Assigning a new
value to this property also replaces the value of the placeholder
property with the same string data, albeit without any formatting
information. Assigning a new value to this property does not affect
any other style-related properties of the text field.
It seems that this works for others... I have no idea why it haven't worked for me before... maybe some project settings. Thanks for the comments. Currently I have no way how to test it again.
Obsolete:
But I don't know why, text is applied correctly, but placeholder color remains same (black/gray).
This is an improved version of the extension provided by @Medin Piranej above (good idea by the way!). This version avoids an endless cycle if you try to get the placeHolderTextColor and prevents crashes if the color set is nil.
public extension UITextField {
@IBInspectable public var placeholderColor: UIColor? {
get {
if let attributedPlaceholder = attributedPlaceholder, attributedPlaceholder.length > 0 {
var attributes = attributedPlaceholder.attributes(at: 0,
longestEffectiveRange: nil,
in: NSRange(location: 0, length: attributedPlaceholder.length))
return attributes[NSForegroundColorAttributeName] as? UIColor
}
return nil
}
set {
if let placeholderColor = newValue {
attributedPlaceholder = NSAttributedString(string: placeholder ?? "",
attributes:[NSForegroundColorAttributeName: placeholderColor])
} else {
// The placeholder string is drawn using a system-defined color.
attributedPlaceholder = NSAttributedString(string: placeholder ?? "")
}
}
}