Set UITextField placeholder color programmatically
1 - Create an AttributedString with colour you want.
2 - Set this AttributedString to the textfield attributedPlaceholder property
let placeholder = NSAttributedString(string: "Some", attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
let textField = UITextField(frame: CGRect(x: 0, y: 0, width: 100, height: 30));
textField.attributedPlaceholder = placeholder;
self.view.addSubview(textField)
From apple documentation
var attributedPlaceholder: NSAttributedString!
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.
Change UITextField's placeholder text color programmatically
when user does not write any thing in textfield. then put this text as a textfield.text text and change font color.
Changing Placeholder Text Color with Swift
You can set the placeholder text using an attributed string. Just pass the color you want to the attributes
parameter.
Swift 5:
let myTextField = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 30))
myTextField.backgroundColor = .blue
myTextField.attributedPlaceholder = NSAttributedString(
string: "Placeholder Text",
attributes: [NSAttributedString.Key.foregroundColor: UIColor.white]
)
Swift 3:
myTextField.attributedPlaceholder = NSAttributedString(
string: "Placeholder Text",
attributes: [NSAttributedStringKey.foregroundColor: UIColor.white]
)
Older Swift:
myTextField.attributedPlaceholder = NSAttributedString(
string: "Placeholder Text",
attributes: [NSForegroundColorAttributeName: UIColor.white]
)
iPhone UITextField - Change placeholder text color
You can override drawPlaceholderInRect:(CGRect)rect
as such to manually render the placeholder text:
- (void) drawPlaceholderInRect:(CGRect)rect {
[[UIColor blueColor] setFill];
[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];
}
Add Placeholder to UITextField, how to set the placeholder text programmatically in swift?
You need to get the phone number from your database first (convert them to String), then you set placeholder
of your textField
to that String, like so
textField.placeholder = phoneNumberString
Related Topics
Integer Literal Overflows When Stored into 'Int'
Avoid Automatic Framework Linking in Swift
Closure:Use Unresolved Identifier 'Self'
Using Decodable with Inheritance Raises an Exception
Pattern Variable Binding Cannot Appear in an Expression
Navigationview Bar Material Invisible on iOS 15
Protocol Can Only Be Used as a Generic Constraint
Hex String to Text Conversion - Swift 3
Can't Create Default Closure Parameter in Array Extension Method in Swift
Computed (Nsobject) Properties in Swiftui Don't Update The View
How to Get Notification Authorization Status in Swift 3
Create a Navigationlink Without Back Button Swiftui
How to Use Nsvisualeffectview to Blend Window with Background
Nonfailable Enum Initializer with Default Value
How to Customize the Title/Subtitle Font in Callout from Mkannotationview or Just Hide Them