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]];
}
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]
)
UITextField placeholder text is unreadable in iOS13 dark mode
It turns out Apple has provided a way to override this on various elements (or even your entire app's UIWindow) with the following (Objective-C):
if (@available(iOS 13.0, *)) {
textField.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
}
I applied it to all UITextFields via swizzle, to turn it off on EVERYTHING in your project, just use this in your appDelegate didFinishLaunching method but replace textField
with _window
(IMPORTANT EDIT: with the newest version of xCode _window seems to have been dropped and now app projects create something called a SceneDelegate
and the overrideUserInterfaceStyle
has to be applied to that somehow, but I'm new to scene delegates and don't know how they work so I can't offer much help there, to disable scenedelegate and return to traditional AppDelegate management of the UIWindow, see here: https://stackoverflow.com/a/57467270/2057171)
Change placeholder text color on swift
You can set the placeholder text using an Attributed string. Set color to attributes
Property.
textField.attributedPlaceholder = NSAttributedString(string:"placeholder text",
attributes:[NSForegroundColorAttributeName: UIColor.yellowColor()])
Swift 5
textField.attributedPlaceholder = NSAttributedString(string:"placeholder text", attributes:[NSAttributedString.Key.foregroundColor: UIColor.yellow])
Related Topics
Wkwebview Origin Null Is Not Allowed by Access-Control-Allow-Origin
Ios Uiimagepickercontroller Result Image Orientation After Upload
Getting the Difference Between Two Dates (Months/Days/Hours/Minutes/Seconds) in Swift
Save Generated Gif to Camera Roll
How to Add Pagecontrol Inside Uicollectionview Image Scrolling
React Native App Crashes on Launch Screen on Device
Best Way to Check If Object Is Out of Bounds in Array
Swift Tableview in a Uiview Not Displaying Data
What Happens When Testflight App Expire
How to Change Button Title Alignment in Swift
Iphone 6 and 6 Plus Media Queries
How to Programmatically Send Sms on the Iphone
Vertically Align Text to Top Within a Uilabel
Uiwebview Open Links in Safari