UITextField hide all characters - ios

Our customers want a Textfield for a Password which is not showing any character while typing.
Is there any Setting in iOS to activate this behaviour or is it possible to hide also the last typed character using the secureTextEntry property on a UITextField?

You can set your view controller as delegate for your text field, and add the following method, this will not allow the text field to show that last character:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == self.pinTextField) {
NSString *currentText = textField.text;
NSString *newText = [currentText stringByReplacingCharactersInRange:range withString:string];
textField.text = newText;
return NO;
}
return YES;
}

please check below image attachement.
When you put uitextfield in your storyboard or xib. then after select that uitextfield & you can see the properties list in your right side of xcode. Now you can see the second image type of propeties in your properties list. Now true check box for secure text entry. when you select it then after you can see the first image type of entries in you app.

Related

How to create UIView to show text like in Terminal?

I'm making telnet program and I have everything resolved but the text output.
I want it to have console look and feel, and basic controls like UITextField or UILabel do not work at all for this.
Is there any custom control to do this?
How can I write one myself?
You can use UITextView to display text and UITextField to input the text and override textFieldShouldReturn: method:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSString* inputString = textField.text;
[inputString lowercaseString];
// when you type 'clear' clear output view
if ([inputString isEqualToString:#"clear"])
{
// Your text view outlet to display the data
[self.outputTextView clear];
}
else
{
[self.outputTextView setText:inputString concatenate:YES];
// Your text field outlet to input the data
[self.inputTextField setText:#""];
return YES;
}
Remember to set up text field delegate for input text field.

How to show Xbutton(clear button) always visible in uisearchbar [duplicate]

This question already has answers here:
Adding the "Clear" Button to an iPhone UITextField
(11 answers)
Closed 2 years ago.
In my application, I am adding a UISearchBar.
My intent is to enable the UISearch Bar "X button"(clear button in UITextField) to be always visible.
I have tried using the following code below to try to make the "X Button" be always visible. However, it does not work. If I set tf.clearButtonMode = UITextFieldViewModeNever, the clear button in uitextfield not showing. I am not sure what is wrong?
I would really appreciate anyone's help here. Why is this not working?
Code (Not working)
for (UIView* v in searchBar.subviews)
{
if ( [v isKindOfClass: [UITextField class]] )
{
UITextField *tf = (UITextField *)v;
tf.delegate = self;
tf.clearButtonMode = UITextFieldViewModeAlways;
break;
}
}
Goal:
I want to always show the clear button if the text length is equal to 0
i.e. if I don't input any text.
UITextField *searchBarTextField = nil;
for (UIView *subview in self.searchBar.subviews)
{
if ([subview isKindOfClass:[UITextField class]])
{
searchBarTextField = (UITextField *)subview;
searchBarTextField.clearButtonMode = UITextFieldViewModeAlways;
break;
}
}
This is the default behavior of the search bar. Because if the UITextField is blank then there is no need to press it.
U can do it in Xib. I am attaching the screenshot.
And programmatically
myUITextField.clearButtonMode = UITextFieldViewModeAlways;
I tried to get it but unfortunately , There is no Way of Customising with the ClearButton(X) of UITextField .
There is a way that If You only need it to get resign the KeyBoard , Then just overriding this method :
Just clear the field yourself and call resignFirstResponder .
-(BOOL)textFieldShouldClear:(UITextField *)textField
{
textField.text = #"";
[textField resignFirstResponder];
return NO;
}
Documentation about it HERE
This is an older question but I came here with an equal customer request: "Show the clearButton as soon as the cursor is in the searchField. We want to be able to cancel the search with this button in any stage".
I came up with a solution other than adding a custom button:
AppleDocs:
UITextFieldViewModeAlways The overlay view is always displayed if the
text field contains text.
So adding a whitespace as the first character will set the clearButton active.
The leading whitespace can be removed as soon as text is entered in the searchField or at any other point before using the text.
-(void)textFieldDidBeginEditing:(UITextField *)textField{
//adding a whitespace at first start sets the clearButton active
textField.text = #" ";
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
...
NSString *completeNewString = [textField.text stringByReplacingCharactersInRange:range withString:string];
//remove the dummyWhitespace (here or later in code, as needed)
self.searchString = [completeNewString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
...
return YES;
}

one TextField to 6 others

Quick Question, I hope,
I have a UITextFeild that I want to type in an number and have that number populate into 6 other UITextfields on the same VC.
The first textfiled is called percentage goal while the others are named endmonth1year1percentage, endmonth2year1percentage, endmonth3year1percentage, etc.
I am currently using iOS6 with storey board.
Any help is appreciated. Thanks.
Detect the change in the first text field: UITextField text change event
And then update the text property of other text fields you want to be populated.
try like this,
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//replace your textfield names here
textField1.text=textField.text;
textField2.text=textField.text;
textField3.text=textField.text;
textField4.text=textField.text;
textField5.text=textField.text;
return YES;
}
So for the first UITextField you must set the delegate the class that is responsible with the display of the text fields (a view controller or a custom view). For the other UITextFileds you should set a tag like 1,2,3..for each UITextFiled (because you say that will be lots of UITextFields)
After you set the delegate for the first UITextField and setup the tags, you can implement two different delegate methods (depending on what you want).
The method that Sunny provided, this is for instant changes:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
for(int i = firstTextViewTag; i<=lastTextViewTag; i++) {
UITextView *newTextView = (UITextView *)[self.view viewWithTag:i];
//or [yourCustomView viewWithTag:i]
newTextView.text = textField.text;
}
return YES;
}
Second, you can use the following if you want to update the textfields only after the user finished typing and the keyboard is hidden:
-(void)textFieldDidEndEditing:(UITextField *)textField {
for(int i = firstTextViewTag; i<=lastTextViewTag; i++) {
UITextView *newTextView = (UITextView *)[self.view viewWithTag:i];
//or [yourCustomView viewWithTag:i]
newTextView.text = textField.text;
}
}
EDIT
So first of all delegate is a pattern heavy used by iOS here is a tutorial that will explain the basic concept of delegate.
Second, some of the UI controls that iOS provide have a delegate instance (after you read the above tutorial you will understand why and how it's working). A class can be a delegate of a custom UI control only if the class implements the required methods that the delegate provides (NOTE: there are optional methods also in the delegate), if the class doesn't implement the required methods a build warning will be shown at the line where the delegate is set.
Third, the method used in this answer are delegate methods of the UITextFiled (check apple docs)
I lost count, [tag][3] is a property available for all UIViews of subclasses of UIView that can be used to identify an object, but be careful that this property by default is 0 so make sure when you set the tag property you will use a value > 0.
For more details please use google and Apple Docs

iOS 6 - Responding to Keyboard Done Button State

In the iOS Apprentice Series eBooks, you are supposed to build a Checklist app. The tutorials have you go through and make a Done button on a button bar that is disabled and enabled based on the text inside of a UITextField object.
- (BOOL)textField:(UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newText = [theTextField.text stringByReplacingCharactersInRange:range withString:string];
self.doneBarButton.enabled = ([newText length] > 0);
return YES;
}
Now, the keyboard is set to disable the Done button (on the keyboard) if the text field is empty. Is there a way to observe the state of the keyboard done button and have the done button bar button reflect it accordingly?
IE, when the keyboard done button is enabled, the button bar done button is enabled.
UITextField supports what you want through the enablesReturnKeyAutomatically property. This property is from the UITextInputTraits protocol.
When you create your UITextField, set this property.
self.textField.enablesReturnKeyAutomatically = YES;
This means the Return key (whatever it is labeled) will automatically becomes disabled if the text field is empty. And it automatically becomes enabled when text is entered.
There is no way to observe the state of this so you must implement the code you already have for textField:shouldChangeCharactersInRange:replacementString: to update your other Done button.
You may add a target on your text field with the following events.
UIControlEventEditingDidBegin
UIControlEventEditingChanged
UIControlEventEditingDidEnd
UIControlEventEditingDidEndOnExit
UIControlEventAllEditingEvents
Example:
In viewDidLoad
[_textField addTarget:self action:#selector(textFieldEditing:) forControlEvents:UIControlEventAllEditingEvents];
Action Method:
- (void)textFieldEditing:(id)sender
{
_doneButton.enable = ([[sender text] length]>0);
}

I need to validate two uitextfields differently. How do I do this?

One text field only takes alphabet characters and the other one is alphanumeric. I have defined shouldChangeCharactersInRange in my ViewController for one of them. Now I don't know where I'm supposed to define the logic for other uitextfield.
Can someone help me understand how this should work?
If they both have the same delegate, then they would both be validated in the same shouldChangeCharactersInRange method. You need to put an if-else clause in that method to check which text field is the sender. You need IBOutlets for the two text fields so you can do the comparison.
You have a couple options. If you have created iVars for your text fields, checking which one is calling shouldChangeCharactersInRange is as simple as ==. Below I've also shown that another option would be to assign tags to the text fields and check the tag of the sending text field.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//Your first option
if (textField == myFirstTextField) {
//
}else{
//
}
//another option, if you don't want to create iVars you can assign tags to your text fields and do this
if (textField.tag == 99) {
//
}else{
//
}
}

Resources