0
votes

I have another object that I want to have the same color as the UITextField's default placeholder text color.

I know I can simply make a new UIColor with the same color, but this seems hacky and problematic if Apple changes the default UITextField color. I instead am looking for the proper way to access the UITextField color so that I can set another field to the same color?

Thanks.

2

2 Answers

0
votes

From UITextField API in UIKit, the place holder color of UITextField is 70% of gray color and it is defined by the iOS SDK.

open var placeholder: String? // default is nil. string is drawn 70% gray

From Apple Doc.s

The placeholder string is drawn using a system-defined color.

Probably, we may not have an access to these system defined colors with any attributes given in iOS SDK.

0
votes

Try this:-

@IBOutlet weak var textField: UITextField!

Now in viewDidLoad method:-

let placeHolederText = "PlaceHolder"
let attributedPlaceHolder = NSAttributedString(string: placeHolederText, attributes: [NSAttributedString.Key.foregroundColor : UIColor.red ,NSAttributedString.Key.font : UIFont.systemFont(ofSize: 12) ])
self.textField.attributedPlaceholder = attributedPlaceHolder
self.textField.borderStyle = .bezel
self.textField.tintColor = .gray

Result:-

enter image description here

I hope this will help you, Thanks.