iOS: Make a UITextField like WhatsApp Verification Code TextField? - ios

My problem is that I want to make a UITextField like WhatsApp verification code textField(like below image).Where default value contains number of character to be entered(as hint for user). When user enter text replace hint character with entered character one by one?
Any help is much appreciated.

Please refer below link of my previous answer. Hope you will get an idea how to do this. Only single UITextField, No third party library use.
link- https://stackoverflow.com/a/36769911/5097148
for don't use of paste make couple of changes-
add myLable above textField then add this code in viewDidLoad()
self.myLable.userInteractionEnabled=true;
UITapGestureRecognizer *LongPressgesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapPressgesture:)];
tapPressgesture.delegate=self;
[self.myLable addGestureRecognizer:tapPressgesture];
and
- (void)tapPressgesture:(UITapPressGestureRecognizer *)recognizer
{
[self.txtField becomeFirstResponder];
}
for background like your image you can set textField background image property or add bottom border to your textfield. UITextField border style must be UITextBorderStyleNone If you have any issue then let me know.
Happy coding:)

After adding 6 Textfields and background image.
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
if (textField==textfield1)
{
[textfield1 resignFirstResponder];
[textField2 becomeFirstResponder];
}
else if (textField==textfield2)
{
[textfield2 resignFirstResponder];
[textField3 becomeFirstResponder];
}
}
continue till textfield 6

Related

Detect when a user presses the space button

I currently have my keyboard set so the type is Numbers and Punctuation [so users can type in a hypen symbol]. Does anyone know how to detect if the user presses the space bar so I can automatically set the style to a different keyboard type [in my case letters] so I don't have to build a custom keyboard?
Try this code - I am sure that it will detect if a space character is coded in - I have not really tested how well the keyboard switch works. You should also try it out without the resignfirstresponder becomefirstresponder code once the code below is working. Good Luck! (PS your viewCOntroller needs to be declared as a UITextview or textfield Delegate)
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText: (NSString *)text_message {
if ([text_message isEqual:#" "]) {
// Put your code to change the keyboard then refresh the screen.
[textView resignFirstResponder]; //close textview or textfield
[textView setKeyboardType:UIKeyboardTypeDefault];
[textView becomeFirstResponder]; // opens keyboard for textfield or textview
[self.view setNeedsRefresh]; // refresh view
// return NO;
}
return YES; //go back to editing
}

Using next and previous button with UITextField, problems with firstresponder

I am using a subclass of a scrollview that moves the keyboard out of the way. (TPKeyboardAvoidingScrollView)
I think this is conflicting with my implementation of the next and previous buttons. I have built an inputaccessoryview
I have a category that sets the next and previous textfields for each field
when i edit a textfield, i set the current, previous and next textfields
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
_currentTextField = textField;
_prevTextField = [textField prevTextField];
_nextTextField = [textField nextTextField];
return YES;
}
when they click next or previous i call this method
- (void)selectAdjacentResponder:(id)sender{
UISegmentedControl *segmented = sender;
if(segmented.selectedSegmentIndex == 0){
[_prevTextField becomeFirstResponder];
} else {
[_nextTextField becomeFirstResponder];
}
}
this works fine.. but when i close the keyboard. my scrollview is messed up. if i add the line
[_currentTextField resignFirstResponder];
to the first line of my selectadjacent method it solves the problem. but the problem is it makes the screen focus in a funky way since i'm dismissing and accessing the next textfield at the same time.
i have tried resigning first responder when i close the keyboard. but i think my scrollview is calculated before that point.. any idea what to do =/

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;
}

UITextView double tap to edit

So I have a UITextView I'm using to make a moveable, editable label (My prior searches on SO showed this to apparently be the best option). I need to have the user double tap to enable editing and set it to become the first responder. I can't find a way to do this, and all my other searches have turned up either outdated answers or vague answers. Nothing I've tried seems to work. I've tried using the UITextViewDelegate to have it start editing as opposed to selecting text using textViewDidChangeSelection:, but it doesn't work until you change the current selection. I also tried using a custom UITapGestureRecognizer like so:
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doubleTap:)];
[doubleTap setNumberOfTapsRequired:2];
[doubleTap setNumberOfTouchesRequired:1];
[newLabel addGestureRecognizer:doubleTap];
-(void)doubleTap:(UITapGestureRecognizer *)sender
{
NSLog(#"Double tap detected");
NSLog(#"Sender view of class %#", [[sender view] class]);
UITextView *tappedView = (UITextView *)[sender view];
[tappedView setEditable:YES];
[tappedView becomeFirstResponder];
// [tappedView setEditable:NO];
}
The double tap gesture is never called. I'm not sure why. Strangely, it also doesn't select text either while it's like that. It seems to just break double tap gestures. Is there a way to get rid of the standard double tap selection gesture, or to modify it? Should I subclass UITextView and, if so, what would I change?
I found a neat solution, with less code (Swift 4.2):
Create a Custom UITextView Class and in it write this
override var canBecomeFirstResponder: Bool {
if self.isEditable {
return true
} else {
self.isEditable = true
return false
}
Then in viewDidLoad() write the following (or deselect 'Editable' under behaviour in the storyboard editor)
myTextView.isEditable = false
Then simply put the same code somewhere when you're finished editing the text - eg. textViewDidEndEditing(_ textView: UITextView)
myTextView.isEditable = false
When the view loads, the textView is not editable and all data detectors work. A second tap then allows it to be edited (and disables the data detectors).
As soon as you exit the textView, it's set to not editable again and the data detectors work.
All this without any gesture recognisers! :-D
Sublassing UITextView, I added this method to the .m file.
-(BOOL)canBecomeFirstResponder
{
if (self.editable == YES){
return YES;
}
else{
return NO;
}
}
In addition to this, I used
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGesture
{
return YES;
}
This is the best way I found to solve my problem. I only wanted to allow double tapping to edit. I wanted no text selection, scrolling, etc to happen until it was double tapped. To futher finish this, you'll need a to use a UITextViewDelegate to turn textView.editable = NO
-(void)textViewDidEndEditing:(UITextView *)textView
{
[textView setEditable:NO];
}
You should set the target of the Gesture Recognizer as the textfield.
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:textField action:#selector(doubleTap:)];

resignFirstResponder programmatically [duplicate]

This question already has answers here:
Understanding resignFirstResponder with UITextField
(3 answers)
Closed 9 years ago.
How do I remove a keyboard from the view as a result of the return key being touched when the UITextField was created programmatically.
If the UITextField was called in the viewDidLoad I know how to do this, but the UITextField was created as a result of an -(IBAction).
I created my UITextField programmatically. I know the resignFirstResponder removes the keyboard. I have it set up to do so when the screen is taped outsie the keyboard. I also have it working to where if the user triggers the IBAction with the UIButton related to the UITextField the keyboard goes away. I also want to be able to hide the keyboard when the user selects return from the keyboard.
You need to make yourself a UITextFieldDelegate and implement:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
Make sure you set the textField's delegate to self when you create it.
You can use this:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
You'll need to set your text field's delegate to self first, though:
self.textField.delegate = self;
Or, you could right-click-drag from the text field in IB to little orange circle at the bottom.

Resources