Change UIKeyboardType based on text input - ios

I'm trying to change the UIKeyboardType to the alphabet keyboard when the user types a space, mirroring the effect of typing an apostrophe. However, my code won't change the keyboard appearance until the user dismisses the keyboard and then brings it back again.
Edit: To clarify, the keyboard type starts as UIKeyboardTypeNumbersAndPunctuation and I want to change to ASCIICapable, because the typical user input is in the form of "# cups flour". I realized that the ASCIICapable keyboard has this functionality built-in, so presenting the ASCII capable keyboard but showing the numbers/punctuation first would work.
Here's my code:
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([string isEqualToString:#" "]) {
textField.keyboardType = UIKeyboardTypeASCIICapable;
}
return YES;
}

dismiss the keyboard and then become the first responder again. In my code, I created a IBOutlet for the textfield *tf. It worked.
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([string isEqualToString:#" "]) {
self.tf.keyboardType = UIKeyboardTypeNumberPad;
[textField resignFirstResponder];
[self.tf becomeFirstResponder];
}
return YES;
}

Related

iOS UItextfield reload after key hit

I am making a simple application that has a TextField and a TextView
Whatever we write in textfield will get updated in textview (Both are in a single View, and it is the Root view).
the textfield will get updated every time i hit any key in the keyboard.
I know that the delegate method "textfielddidEditing" helps , but it only helps when we click on the field or click back. I want the method that invokes every time when i hit anything in the keyboard.
Any help is appreciated.
I'm still learning iOS
Please use below textfield delegate method. Its easy and simple to use with all appen and delete mechanism. You can delete multiple characters too by selection. Just store newString value to your textView
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSMutableString *newString = [NSMutableString stringWithString:textField.text];
if (range.length > 0) {
[newString replaceCharactersInRange:range withString:string];
}
else {
[newString appendString:string];
}
NSLog(#"%#",newString);
return YES;
}
You can use UITextField Delegate method. It invokes every time when you hit anything in the keyboard. Write your code as per your requirement..
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.text.length>0)
{
self.yourtextview.text=textField.text;
}
return YES;
}
Hope it helps you..
The above code mentioned by Vidhyanad900 will work. But its if would fail in case a user deletes the text. (Hits backspace or selects complete text and deletes it)
Hence Modifying the code
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
int iNewLength = [textField length];
if([text isEqualToString:#""]) //If text is deleted
{
if([textField selectedTextRange]) //If Text is Seleceted And Deleted
iNewLength = [textField length] - [textField selectedRange].length;
else
iNewLength = [textField length] - 1;
}
if (iNewLength > 0)
{
self.yourtextview.text=textField.text;
}
return YES;
}

how to erase or clear UITextField

how to clear text field in ios,
How can I make a textfield box remove all content on the users first keypress?
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([tfieldDOB.text length] == 4)
{
tfieldDOB.text=[NSString stringWithFormat:#"%#/",tfieldDOB.text];
}
else if([tfieldDOB.text length]==7)
{
tfieldDOB.text=[NSString stringWithFormat:#"%#/",tfieldDOB.text];
}
return YES;
}
change the textfield attribute clear button mode in appears while editing
or other choice just use the single line, where you need to add
yourtextfieldname.text=#""; //it is used for clear the textfield values
Swift
yourtextfieldname.text=""
or another way
clearField =#"YES";
if([clearField isequaltostring:#"YES"]) //check this line in
{
tfieldDOB.text = #"";
clearField =#"NO";
}
Implement the text field's delegate method textFieldShouldBeginEditing: and set the text as empty string when the text field is just about to being editing.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
[textField setText:#""];
return YES;
}
Or you can set the property clearsOnBeginEditing of the textfield as
[textField setClearsOnBeginEditing:YES];
and it will clear the text when editing begins

Close text field when a number of characters is inserted

I have a text field where I want to insert a phone number. The problem is that I use number pad and there is not any enter key and I without that key I don't know how to close the text field.
Is there any way of checking how many number os characters there are in a text field and close the number pad when there are X chars?
Sorry for my english and sorry for the noob question but I am just starting with ObjC.
Thanks *
Sorry guys, I forgot to tell you that I already have tried this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField.text.length== 9) {
[textField resignFirstResponder];
}
return YES;
}
Yes, you have two choices in my opinion, you can:
A. Check the number of chars returned in delegate method shouldChangeTextInRange and then if your limit is reached resign first responder (when you resign first responder the keyboard dismisses,
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ( textView.text.length + (text.length - range.length) == 10) //or whatever value you like
{
[textField resignFirstResponder];
}
}
Or B:
Override the touchesEnded method of your view and call resignFirstResponder, so that when the user touches the view outside the textField, the keyboard dismisses. - this is what I do in my app.
Like this:
Just add this method to your viewController, it will be called automatically when the touch in the view ends, in this method you simply resignFirstResponder and the keyboard will disappear.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[textField resignFirstResponder];
}
I think it's better to do this in touchesEnded than began, it 'feels' nicer to have the keyboard dismiss when you lift your finger from the view, rather than when to initially touch it.
I found that resignFirstResponder do not include the last char typed. I had to do it in this way,
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
DLog(#"Existing: %#, string: %#, range:(%lu, %lu)", textField.text, string, (unsigned long)range.location, (unsigned long)range.length);
if (textField == field2.textField) {
if ( textField.text.length + (string.length - range.length) == kMyNumberLength) // e.g. 10
{
textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
[textField resignFirstResponder];
}
if (textField.text.length + string.length - range.length > kMyNumberLength) {
return NO;
} else {
return YES;
}
}
return YES;
}
Swift
when reaching the maximum number of characters, add the new char to the current text in the textfield and then dismiss the keyboard
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let maxLength = 5
let currentString: NSString = textField.text! as NSString
let newString: NSString = currentString.replacingCharacters(in: range, with: string) as NSString
if newString.length == maxLength {
textField.text = textField.text! + string
textField.resignFirstResponder()
}
return newString.length <= maxLength
}
Every time a user adds a number, the delegate of your text field will be notified through this method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger length = [[textField text] length] - range.length + string.length;
if (length == ...) ...
}
You can set up your delegate in such a way as to watch the length of the modified content of your text field, and close the field when the expected length is reached.
However, this has a high potential to frustrate your users a lot: they would make a mistake entering the last character every now and then, and the field is going to close on them, not letting them correct the problem. A better approach is to dismiss the pad when users tap away from your text field, which keeps end-users in control of what is going on:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[textField resignFirstResponder];
}
You can just put a toolbar with a Done on it, on the click on that button you can hide keyboard and toolbar as well.
-(IBAction)doneButtonClicked:(id)sender{
[yourTextField resignFirstResponder];
[toolBar setHidden:YES];
}
or you can use this custom tool
Had issues with some of the suggestions above not retaining the last character entered.
I found this worked well for me.
Method simply examines the length of the text as it is building up and then runs [textField resignFirstResponder] to dismiss.
All usual textField delegates will run for the textField at point field loses focus and keyboard is dismissed.
- (void)textFieldDidChangeSelection:(UITextField *)textField {
NSLog(#"textField.text: %#", textField.text);
if (textField.text.length == 4) // If 4 is your max-length
[textField resignFirstResponder];
}
Use this textfield delegate method to find out the length of textField.text
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
int textLength = [textField.text length] + 1;
return YES;
}

Adding text to UITextField you can't delete

What I would like to do is to add some text at the beginning of UITextField that cannot be deleted. I add this text when the user taps on UITextField like this:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField.text.length == 0) {
textField.text = #"myText";
}
return YES;
}
How do I prevent the user from deleting this text in a normal way? (without adding a separate UILabel on top of the TextField, for example).
Thanks in advance!
You can implement this code in shouldChangeCharactersInRange delegate method.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.text.length == 1 && [string isEqualToString:#""]) {//When detect backspace when have one character.
textField.text = #"myText";
}
return YES;
}
Checkout shouldChangeCharactersInRange in the UITextFieldProtocolReference
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
the text field calls this method whenever the user types a new
character in the text field or deletes an existing character.
So, you have control on whether text actually gets changed so you can insert your custom logic in that delegate callback enforcing whatever rules about what can change.
Use this
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([[textField text] length]==LENGTH_OF_TEXT && [string isEqualToString:#""])
{
return NO;
}
else
return YES;
}
Hope it helps you.

UitextField shifts control to next textField upon using keyboard input

I am new to iPhone programming. I have two textfields in iPhone with numberPad Keyboard type and i am trying to implement a simple logic that on typing a single digit using numberPadKeyBoard, the control should shift to next textField i.e. second textfield should become FirstResponder. I don't know how to implement this. Please guys any help would be appreciated.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if(textField == urFirstTextField) {
[urFirstTextField resignFirstResponder];
[urSecondtextField becomeFirstResponder];
}
}
UPDATE
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if(textField != urFirstTextField)
{
[textField resignFirstResponder];
[urFirstTextField becomeFirstResponder];
return NO;
}
return YES;
}
Suppose you have 2 text field textField1 and textField2 then implement the delgate methods of the UITextFieldDelegate as
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if(textField == textField1)
{
[textField1 resignFirstResponder];
[textField2 becomeFirstResponder];
}
return YES;
}
go through the delegate method of UITextField named "textField:shouldChangeText:inRange:" ..... check the length of your textField and make second textField as first responder before returning YES;
Or you can play with a lot of other delegate methods defined

Resources