Disable a particular key in UI Keyboard - iphone

It need to disable '&' key from number and punctuation keyboard, so is it possible to disable a particular key in UIKeyboard?

I don't think it's possible to disable a certain key (unless it's one of the action keys such as the return key) but if you are using a UITextField you can use the - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string delegate method to see if the user pressed the & key and remove it from the string

You cannot do that. However your options are:
create your own custom keyboard not offerring '&' key (too much effort IMO)
If you use UITextField you can validate the text submitted by user: remove '&' and/or inform user that it is not allowed to use '&' (much easier).
EDIT: you can also connect UITextField's "Editing Changed" event to the File's Owner's IBAction and filter out '&' there.

There is one delegate method for textField in which you can block specific characters if you want based on their ASCII values. The method can be written as follows:
-(BOOL)keyboardInput:(id)k shouldInsertText:(id)i isMarkedText:(int)b
{
char s=[i characterAtIndex:0];
if(selTextField.tag==1)
{
if(s>=48 && s<=57 && s == 38) // 48 to 57 are the numbers and 38 is the '&' symbol
{
return YES;
}
else
{
return NO;
}
}
}
This method will permit only numbers and & symbol to be entered by the user. Even if the user presses other characters they won't be entered. And as it is a textField's delegate method you don't need to worry about calling it explicitly.

//Disabling the '<' '>' special characters key in Keyboard in my code
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSCharacterSet *nonNumberSet = [NSCharacterSet characterSetWithCharactersInString:#"<>"];
if (range.length == 1)
return YES;
else
return ([text stringByTrimmingCharactersInSet:nonNumberSet].length > 0);
return YES;
}

Related

Keypressed event in UITextField

I have 10 textfields, in which I could enter only one character in each textfield. After a character is entered in each textfield, the focus should move to the next one. Similarly when i delete character from a textfield by pressing the backspace or delete, i need to get the focus to the previous textfield. If I could get the keypressed event, I could do that. Right now I am not able to find any keypressed event examples.
Implement UITextFieldDelegate.
Implement the delegate methods in the protocol. You can achieve the things you wanted.
You can set the focus by using the method becomeFirstResponder to the required textfield.
Have a look at the delegate method
- (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 that could solve your problem.
Based on Aadhira's answer, but taking into account Kirk Woll's comment, you can generate what the latest text will be by using stringByReplacingCharactersInRange:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *value = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSLog(#"value: %#", value);
return YES;
}
Just to give you directions:
Assign tag to each text field.
Implement UITextFieldDelegate. There are all the methods you need to detect any event that takes place inside the text field. In each method you can check the tag and move focus properly.
Hint: you can use [mainView viewWithTag:XX] to quickly pick the text field you need.
Each time the text is changed you can check the text property of the text field and it will give you the answer which button was pressed.
you have to implement the UITextFieldDelegate protocol into your code and this method will tell you when you start begin editing in text field
– textFieldShouldBeginEditing:
and you can set the if condition in this method according to your requirement...
You have to use the textfieldDelegate methods.
In your textFieldShouldReturn method you have to set your responders like
if (textfield == textField1)
{
[textField2 becomeFirstResponder];
}
else if (textField == textField2)
{
[textField3 becomeFirstResponder];
}
else
{
[textField3 resignFirstResponder];
}
return YES; // as method return type is BOOL.

delete last character UITextField

I have an UITextField and I would like that for every tap on a character, the first character is deleted. So that I just have one character in my textField every time. Moreover I would like it to display every tap in the console log.
How can I do this?
You need to implement shouldChangeCharactersInRange method in your text field delegate:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:
(NSRange)range replacementString:(NSString *)string{
textField.text = #"";
return YES;
}
You may need to check for range and string values to cover all possible cases (like copy/paste actions). This code just sets the text field's value to the last typed character.
UITextField inherits from UIControl, so you can use the target-action mechanism that is part of the UIControl class:
[textField addTarget:self action:#selector(updateTextField) forControlEvents:UIControlEventValueChanged];
In the action method, you can replace the UITextField's text with only the last character and log that character in the console. Note that since changing the UITextField's text will again result in the "updateTextField" message being sent a second time to the target, you will need some kind of mechanism for determining whether to update or not:
- (void)updateTextField {
if(updateTextField == YES) {
updateTextField = NO;
NSString *lastChar = [textField.text substringFromIndex:[textField.text length]];
[textField setText:lastChar];
NSLog(#"%#", lastChar);
} else {
updateTextField = YES;
}
}
Or something like that anyway...
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField.text.length > 8) {
return NO;
}
return YES;
}

iPhone Keyboard Filter Question

I want to limit the character users can type in iPhone's keyboard, so I created an array of my own. e.g. The array including 0~9 and a dot to enable users to type a price. Then I can return NO for -(BOOL)textField:shouldChangeCharactersInRange:replacementString: if the replace string is not in the array.
The problem is that the backspace button is also disabled when I use this array to filter text. Any ideas about how to enable backspace button?
Another problem is that I want to let users type their names and therefore I don't want to let them switch to numbers and punctuaction (backspace button is also locked if I use an array to filter). How to disable the switch button on the keyboard (Now I just limit them to type a~z, blank and "." , but I think disable the switch button might be a better way)?
I find a way (maybe not good enough, but it can do the work for backspace function):
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField == txtChargeAmt)
{
if(string.length == 0) //backspace button is pressed
{
textField.text = [textField.text substringToIndex:(textField.text.length - 1)];
return NO;
}
for(NSString *s in arrNumberAndDot)
{
if([string isEqualToString:s])
{
return YES;
}
}
return NO;
}
else
return YES;
}
Other ideas about the backspace issue are welcomed. And how to disable the switch button then?
I guess I am not sure why you would want to use the same TextField for these two different types of input.
I would have two fields, an alphanumeric field for name entry and a numeric field for number entry.
Or am I not getting your question?
This will do what you want a little more succinctly (and efficiently):
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string
{
NSCharacterSet *validCharacterSet = [NSCharacterSet characterSetWithCharactersInString:#".0123456789"];
BOOL shouldChange =
[string length] == 0 || // deletion
textField != txtChargeAmt || // not the field we care about
[string rangeOfCharacterFromSet:validCharacterSet].location != NSNotFound;
if (!shouldChange)
{
// Tell the user they did something wrong. There's no NSBeep()
// on the iPhone :(
}
return shouldChange;
}
I'd construct that character set somewhere else so you you'd only have to do it once, but you get the idea. Anyone have any thoughts on what to do to alert the user they used an invalid character? I'm trying to solve a similar problem.

Solution for iPhone new file dialog keyboard

i want to let the user type in the name of a new file, so there are certain characters i want to prevent entry on. is there a special keyboard i can use or can i disable certain keys on the iphones keyboard.
is the answer to just run a regular expression on the input text and tell the user the filename is invalid (if so what would that regular expression be?)
ANSWER: (or what i ended up doing)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
BOOL valid;
//if the user has put in a space at the beginning
if ([string isEqualToString:#" "]){
if (range.location == 0){
valid = NO;
}
else{
valid = YES;
}
}
//otherwise test for alpha numeric
else{
NSCharacterSet *alphaSet = [NSCharacterSet alphanumericCharacterSet];
valid = [[string stringByTrimmingCharactersInSet:alphaSet] isEqualToString:#""];
}
//print the warning label
if (valid == NO){
[errorLabel setText:#"Invalid input"];
}
else{
[errorLabel setText:nil];
}
return valid;
}
You can implement the delegate method
For UITextField,
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
For UITextview
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
and decide weather to append the entered characters or not.
You can implement the UITextFieldDelegate protocol and use textField:shouldChangeCharactersInRange:replacementString: to watch the text entry and prevent unwanted characters by returning NO.

How to dissallow certain characters in a UITextview

I have a uitextview that is editable but there are certain characters I would like to be disallowed from being typed.
How can I do that?
You can do this by assigning a delegate to the UITextView, and implementing the following method in the delegate:
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
In the body just write some code that scans through the input text to see if you find the characters you want to filter, if you see them return NO, otherwise return YES.
Unfortunately, this is not that simple, because textView:shouldChangeTextInRange: replacementText: is not necessarilly called with one-character strings. It is for keyboard input, but it isn't when pasting, or when using speech recognition to enter text.
So what do you want to do if the user pastes (or dictates) a string that contains forbidden characters? You might want to let all valid text go through and only delete (or replace) unwanted characters.
The incorrect idea would be to fix the text in the textViewDidChange: delegate routine. While this seems to work, it somehow prevents speech input from working in the UITextView.
The correct idea is to implement textView:shouldChangeTextInRange: replacementText: with full filtering. Here is a sample implementation that filters out newlines:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if([text isEqualToString:#"\n"]) {
[textView resignFirstResponder]; // dismiss keyboard
return NO;
}
text = [text stringByReplacingOccurrencesOfString:#"\n" withString:#" "]; // replace by spaces
NSString *fullText = [textView.text stringByReplacingCharactersInRange:range withString:text];
textView.text = fullText;
return NO;
}
Note that it dismisses the keyboard when the user strikes the enter key. But not quite perfectly: it will also dismiss the keyboard when the user pastes a single newline character. This might be a problem, but this should happen only rarely.