Hide placeholder Programatically using swift 3x - swift

How to hide the placeholder of TextField programmatically using Swift 3x ?

You can not hide the placeholder of UITextField, but you can set it to empty String.
self.textField.placeholder = ""
Now textField doesn't show any placeholder later on if you want to show the placeholder simply set it with String that you want.
self.textField.placeholder = "Enter name"

Related

Button text shrinks when the text is changed

Ones I change the text in button it's getting stink to font size of ~18 even though initial font size is 55. Button is not custom. It has System font.
button.setTitle("Some button text", for: .normal)
I tried manually change font size in code but it doesn't help
button.titleLabel?.font = UIFont.systemFont(ofSize: 55)
I also tried to change the name through configuration function, but there is no font size and it shrinks anyway.
`
button.configuration = paperButtonConfiguration()
func buttonConfiguration() -> UIButton.Configuration {
var config: UIButton.Configuration = .plain()
config.title = "Some button text"
//Should probably be something like this but it doesn't work
//config.font = UIFont.systemFont(ofSize: 55)
return config
}
`
While I was writing this question I kind of solve this, but not completely. If I change the button style from "plain" to "default" it works, but some weird animation of fading and appearance occurs.
How could I do this with "plain"?
UIButton has four type are Plain,Grain,Tinted,Filled .When you create a button in storyboard , button type automatically set with Plain that means new UIButton configurations is on. If you want to old behaviour , you must set style plain to default.
Or , If you want one of the style above . You need to set font like
button.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
var outgoing = incoming
outgoing.font = UIFont.systemFont(ofSize: 50)
return outgoing
}

Localize button Swift

For example I've got an app which has a textLabel and a button which are firstly set as:
mainLabel.attributedText = "labelNewText".uppercased()
mainButton.titleLabel?.attributedText = "buttonNewText".uppercased()
Then I've created a Localization file, where I set a some values for a German language:
"labelNewText" = "Etikette";
"buttonNewText" = "Taste";
And rewrote set ups for label and button like:
mainLabel.attributedText = "\(NSLocalizedString("labelNewText", comment: ""))".uppercased()
mainButton.titleLabel?.attributedText = "\(NSLocalizedString("buttonNewText", comment: ""))".uppercased()
Though, right after after I change my phone language setting to German, the translation only works for a Label but is not working for button. What am I doing wrong and how to localise button titleLabel?
You have to use UIButton's setAttributedTitle:forState: instead of trying to manipulate the text label itself. So for example:
mainButton.setAttributedTitle(myAttributeString, forState: .normal)

Text Field name programatically in swift 4

I've below form:-
I've created this form programatically.
The code for this is below:-
let textFiled = UITextField(frame:CGRectMake(87.0, y, 100.0, 20.0))
textFiled.borderStyle = UITextBorderStyle.Line
textFiled.font = UIFont.systemFontOfSize(8)
Now if I set tag for these text field then it'll store Int value. Which is very tough for me to manipulate this form.
I want to set name like IBOutlet for each text field. So that I can easily handle this form.
Is it possible to do it in Swift 4...
I want Your opinion please....
Similar to tag, we have
accessibilityIdentifier
in which you can add String values
you can just say
textFiled.accessibilityIdentifier = "name"
and get value using the below:-
textFiled.accessibilityIdentifier
UPDATE:
I suggest we never use the accessibilityIdentifier as it's for testing, for your case you should subclass UITextField and add a custom property to identify the text field

Add Placeholder to UITextField, how to set the placeholder text programmatically in swift?

I'm pulling out a phone number from a database, and when the user begins editing in the text field to change that phone number I'd like to use the number I currently have in the database as the placeholder. Since this information changes with each user, how can I set it 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
Swift 3
If your textField has text, you need to first set text property to nil, then set placeholder text:
textField.text = nil
textField.placeholder = "My Placeholder Text"
Important to note for anyone else reading this, setting placeholder text in the main.storyboard seems to nullify this solution, so I had to first clear out my placeholders in the storyboard before implementing this. Once that was done #Khuong and #Himanshu's answer worked perfectly.
Apply this line of code in to View Did Load
new_Password.attributedPlaceholder =
NSAttributedString(string: " New Password", attributes: [NSForegroundColorAttributeName : UIColor.white]) // new_Password : our text feild name
Fetch your desired data from your database (Core data) and after converting it into string format... say phoneString
use this line to set this string as a placeholder text
phoneTextField.placeholder = phoneString
Objective-C code:
[usernameText setPlaceholder:#"My Placeholder Text"];
Just a note to say if you have changed your textfield's text and background colors programmatically, you can't do that with the placeholder text's colors and must set up an Attributed Placeholder instead. This is a problem if your device is in put in "dark mode" and are trying to make a non-dark mode screen by hand - you might not be able to see the placeholder!

How to set text position to bottom in textField

I created a textField through code using the following code
UITextField *txtObj=[UITextField alloc]initWithFrame:CGRectMake(88,100,80,33)];
I am getting the textfield as i planned, but the text we type in that text field seems to appear in the top area of text but not in bottom.I tried to align it but i got tired. Can anyone tell me what's the solution
it should be:
txtObj.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
This property is inherited from the UIControl class. The default is: UIControlContentVerticalAlignmentTop , that's why you get the text on top.
txtObj.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
If you want the text to be in the center you can use ,
txtObj.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
Swift 4,5 solution:
txtObj.contentVerticalAlignment = .bottom