Text Field name programatically in swift 4 - swift

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

Related

Localization the Navigation Bar Title in swift

I write navigation bar title in Attributes inspector. I would like to translate that title when I switch to other language.
self.navigationItem.title = NSLocalizedString("Login", comment: "")
I write it in override func viewDidLoad().
I took navigation item Object ID "S3Z-Mr-Qda" and translate it in Main.strings file.
/* Class = "UINavigationItem"; text = "Package History"; ObjectID = "S3Z-Mr-Qda"; */
"S3Z-Mr-Qda.text" = "Login";
However, it does not change at all and how to implement it in swift?
You can actually provide a translation to the title using that method.
The only thing that you did wrong was setting the property "text". As you can see in the code version, a navigationItem has a title property instead of text.
This would work:
"S3Z-Mr-Qda.title" = "Login";
I think you should put your translation inside a generic
Localizable.strings file instead of the storyboard translation file.
Then use your same code to load the title text:
self.navigationItem.title = NSLocalizedString("Login", comment: "")
Take a look at the official doc in order to create this kind of file:
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/LoadingResources/Strings/Strings.html

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!

Display Views exposed form item label inside selects (Instead of the default '- Any -')?

How to display form item label in Views exposed form instead of '- Any -'? To be more specific I use this code to replace select's default value text with custom text and want that custom text to be the label of that element:
function THEMENAME_form_views_exposed_form_alter(&$form, &$form_state) {
//dpm($form);
if ($form['#id'] == 'views-exposed-form-FORMID') {
$form['ITEMNAME']['#options']['All'] = t('My custom translatable text');
}
}
This works for custom text. What I want is to display its label instead of My custom translatable text with the simple code like:
$form['ITEMNAME']['#options']['All'] = $form['ITEMNAME']['#name'];
but have no luck on such and similar codes to work. According fo $dpm($form) output '#name', '#title' elements seem not to exist at all.
The goal is to have similar functionality of https://drupal.org/project/compact_forms or https://drupal.org/project/In-Field-Labels without another Javascript library (prefer to use couple PHP lines, please no JS solutions)
Your above code will work in case of select field but not for text field. If you need it to work for text fields you can try this
$form['ITEMNAME']['#attributes'] = array('placeholder' => array('My custom translatable text'));
or
$form['ITEMNAME']['#attributes'] = array('placeholder' =>$form['ITEMNAME']['#name']);
hope this helps you

Automatic typing password iphone

In my UIViewController I have 2 textfields. One called passwordtextfield and the other retype. Instead of having people actually retype their passwords I want it to automatically fill it in. Is this possible? I tried something that keeps on crashing.
[_passwordtextfield addTarget:self action:#selector(updateTextField:) forControlEvents:UIControlEventEditingChanged];
- (void)updateTextField:(id)sender{
UITextField *retype = ((UITextField *)_passwordtextfield).text;
}
There is no need to cast your text field. You can assign direct value of your text field.So
Write this line
retypetextfield.text = _passwordtextfield.text;
instead of
UITextField *retype = ((UITextField *)_passwordtextfield).text;
I don't know why you want to do this, as mentioned in the comments, but to answer the question... It looks like you are creating a new UITextField in updateTextField instead of setting the text of one that exists. It should look something like this:
- (void)updateTextField:(id)sender{
retypeFied.Text = _passwordtextfield.text;
}
Substitute whatever you named the pointer to your second field for retypeField.

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