Proper Way to Mimick UITextField Placeholder Color - ios

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.

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.

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:-
I hope this will help you,
Thanks.

Related

How can I set the normal color for a floating placeholder in a MDCMultilineTextField?

I'm having troubles changing the color of the placeholder of a MDCMultilineTextField in the non-active state. I'm using a MDCTextInputControllerUnderline and I've managed to personalize every other part of the textfield, with the exception of the floating placeholder when not active (see pictures below, I need it to be white).
I've tried setting the tintColor of the textfield, the textColor and the tintColor of the placeholderLabel, the normalColor, inlinePlaceholderColor, floatingPlaceholderNormalColor and floatingPlaceholderActiveColor of the controller, but nothing seems to work. What am I missing? What's the attribute to change to set the color of the placeholder?
As suggested in the comment, it was a matter of setting the Attributed Placeholder instead of the regular one.
So, in the end, it was just a matter of doing this:
let stringAttr = [NSAttributedStringKey.foregroundColor: UIColor.white]
let attributedPlaceholder = NSAttributedString(string: placeholder, attributes: stringAttr)
textField.attributedPlaceholder = attributedPlaceholder

Find text color of button in swift4

I am working on app and I give UIButton text color from storyboard (Different for default and selected), I want both text color in a variable.
Anyone help me with this problem.
I have one solution for this problem " Take 'color code' of this color and give it to variable"
Anyone has any other short method for this problem.
Because if I use 'color code' then in future when I change color of button text then Then also work on that 'color code'.
You need to call titleColor() method of UIButton
let color = btnOutletExample.titleColor(for: .normal) or your desired state
try this
let titleColor: UIColor = yourButton.currentTitleColor
At the end of viewDidLoad, you can add the following to capture the color information:
let defaultTextColor = myButton.titleColor(for: .normal)
let selectedTextColor = myButton.titleColor(for: .selected)
You could pull the colour out of the element you configured in your storyboard, using the following code.
let btnTextColor = self.myBtnOutlet.titleLabel.titleColor(for: .normal)

Having textfield background transparent, but placeholder text opaque

Is it possible to have a textfield's placeholder text opaque while its background is set to be transparent, eg. if I choose some background color, and set alpha to some value, like this:
the placeholder text becomes transparent as well. But I want it opaque. So, is this achievable, preferably using storyboard ? Or at least through the code.
If it is unclear what I am trying to achieve, just let me know , and I'll post an image with an example.
You can set the color's transparency instead of the UITextField's alpha. Open the color drop down and select "Other". A color picker will open up. At the bottom of the color picker you can change the opacity.
In Swift you can obtain the placeholder element with:
let placeHolder = textField.value(forKey: "placeholderLabel") as! UILabel
placeHolder.textColor = .blue
placeHolder.isOpaque = true
and make all customizations you prefeer..
You can set the UITextView.background = .clear
I'm providing a little more code so it'll be easier to setup it programmatically, this is a ready-to-use sample.
private lazy var textField: UITextView = {
let textField = UITextView()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.font = UIFont.systemFont(ofSize: 14)
textField.isEditable = false
textField.textAlignment = .center
textField.isScrollEnabled = false
textField.backgroundColor = .clear // this is the line that answer you question
textField.text = """
People with these symptoms may have COVID-19:
Fever of chills, cough, shortness of breath or
difficulty breathing, fatigue, muscle or body aches,
headache, new loss of taste and smell, sore throat,
congestion or runny nose, nausea or vomiting,
diarrhea
"""
return textField
}()
Just remember to set the constraints and add the subview into your superview .addSubview(TextField)

Red Asterisk directly beside placeholder in input box in ios?

I would like to put a red asterix( * ) besides placeholder in ios in the registration form where i am using the uitextfield. I know how to change the placeholder color but in my requirement i have the placeholder color as grey with red asterix( * ) following it. How to achieve this in swift 3? Is it possible to set it in storyboard itself?
You can use the built-in, attributedPlaceholder property of UITextField.
Like this:
let passwordAttriburedString = NSMutableAttributedString(string: "Password")
let asterix = NSAttributedString(string: "*", attributes: [.foregroundColor: UIColor.red])
passwordAttriburedString.append(asterix)
self.textField.attributedPlaceholder = passwordAttriburedString

Swift3 UITextField change placeholder font type and size

Code:
txtField.attributedPlaceholder = NSAttributedString(string:strPlaceHolder, attributes:[NSForegroundColorAttributeName:UIColorFromRGB(color:PlaceHolderColor, alpha:1.0),NSFontAttributeName :UIFont(name: "Didot-Italic", size: 4)!])
I am trying to change the UITextField placeholder font type and font size. It is not working. What is wrong with my code? Any help will be appreciated.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
txtField.attributedPlaceholder = NSAttributedString(string:"Place holder text in here", attributes:[NSForegroundColorAttributeName: UIColor(red: 153.0/255.5, green: 153.0/255.5, blue: 153.0/255.5, alpha: 0.5)])
}
place holder attribute text will be same as your textfield text attribute in size and font
you should change a font attribute of the text field (i.e. txtField.font) and then set your place holder text (i.e. txtField.placeholder)
SWIFT 5 / XCode 11:
UITextField has a property called .attributedPlaceholder you can assign attributes using NSAttributedString Keys. To customize a "searchBoxTextField" placeholder to:
light gray text color &
Times New Roman font of size 20
try copying/pasting this & it should work...
override func viewDidLoad() {
super.viewDidLoad()
searchBoxTextField.attributedPlaceholder = NSAttributedString(string:"Enter a Word" attributes:[NSAttributedString.Key.foregroundColor: UIColor.lightGray, NSAttributedString.Key.font :UIFont(name: "Times New Roman", size: 20)!])
}
note: You can still use Auto Layout selections to control the text appearance entered by the user. So in this example, the placeholder is gray, but if you selected black for the textView text color it will transition nicely when the user types into the box.

Resources