Can I limit the character set for a UITextField? - iphone

I have a bitmap font which which doesn't have every single character, such as accented characters (é î ü etc) or symbols such as "¥" or © which I'm using to display usernames in a Highscore table for a game.
Is it possible to limit the UIKit keyboard to certain characters, or only allow certain characters in the UITextField? Or will I have to roll my own input mechanism? I'm thinking and old school, Arcade style, one letter at a time "thing" would be ok.

Using the UITextFieldDelegate method mentioned by intregus, you can do this quite easily:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// Only characters in the NSCharacterSet you choose will insertable.
NSCharacterSet *invalidCharSet = [[NSCharacterSet characterSetWithCharactersInString:#"abcdefgABCDEFG"] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:invalidCharSet] componentsJoinedByString:#""];
return [string isEqualToString:filtered];
}

You could try using the following UITextFieldDelegate method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;

try using the UIKeyboardTypeNamePhonePad for your UITextField. It only has letters and numbers on it:
textField.keyboardType = UIKeyboardTypeNamePhonePad;

Using Interface builder you can link and get the event for "Editing changed" in any of your function. Now there you can put check for the length
- (IBAction)onValueChange:(id)sender
{
NSString *text = nil;
int MAX_LENGTH = 20;
switch ([sender tag] )
{
case 1:
{
text = myEditField.text;
if (MAX_LENGTH < [text length]) {
myEditField.text = [text substringToIndex:MAX_LENGTH];
}
}
break;
default:
break;
}
}

Related

UILabel have to access keyboard alphabets only

can anyone tell how to access only alphabets through keyboard. Numbers and special characters should not de entered into the UILabel.
I'am new to iOS programming and I'am searching this from last 2 hours of no use. Help me out of this
try this ...
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
{
static NSCharacterSet *charSet2 = nil;
if(textField==txtfirstname)
{
if(!charSet2)
{
charSet2 = [[NSCharacterSet characterSetWithCharactersInString:#"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "] invertedSet];
}
NSRange location = [string rangeOfCharacterFromSet:charSet2];
return (location.location == NSNotFound);
}
}
Hope this helps...
To get a notification you'll have to use the delegate function of UITextField. If you use the UILabel. You might have to go ahead and use the Keyboard notifications. Instead what you can do is use the UITextField. You can use the UITextField border style property to make it look a like UILabel.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:#"0123456789"];
for (int i = 0; i < [string length]; i++)
{
unichar c = [string characterAtIndex:i];
if (![myCharSet characterIsMember:c])
{
return YES;
}
else
{
return NO;
}
}
}

Filter out characters in one UITextField

Ok, I am trying to edit this one text field that is txtCountry to not accept certain letters, but I can't, because the txt country is not setup right. Can someone please fix this up for me. I probably making some kind of little detail mistake.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ([textField isEqual: txtCountry]) {
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:LEAGELNUM] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:#""];
BOOL basicTest = [string isEqualToString:filtered];
return basicTest;
}
}
You don't need to insert the name of the text field into the method. Instead do this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([textField isEqual: txtCountry]) {
//return YES or NO here depending on your criteria
}
}

Remove the last character typed in UItextfield

I need to remove the last character if the textfield length exceeds 100,I used the following code:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textfield.text.length>=100)
{
NSString *text;
textField.text=text;
NSString *newString = [text substringToIndex:[text length]-1];
textField.text=newString;
}
return YES;
}
But it just erases the whole text.
You erase the whole text because of this line:
textField.text=text; // text is nil here
What you wanted to do is more likely the following:
NSString *text = textField.text;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if(textfield.text.length>=100)
{
NSString *text;
text =[[textField text] stringByAppendingString:string];
NSString *newString = [text substringToIndex:[text length]-1];
textField.text=newString;
}
return YES;
}
Paste the code may solve your issue
In iOS 5 or later:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([textField.text length] >= 100)
{
[textField deleteBackward];
}
}
try with the below code
- (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString *)aText {
NSString* newText = [textView.text stringByReplacingCharactersInRange:aRange withString:aText];
// CGSize strSize = [newText sizeWithFont:textView.font constrainedToSize:CGSizeMake(200, 10000) lineBreakMode:UILineBreakModeWordWrap];
if([newText length] > 100)
{
return NO; // can't enter more text
}
else
return YES; // let the textView know that it should handle the inserted text
}
try this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
return !([newString length] >100);
}
In order to fix the UITextField length you don't have to work with strings concatenation. Just use this simple code:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return !([string length]+[textField.text length] > FIXED_LENGTH);
}
This way you, as UITextFieldDelegate, are saying: "change the UITextField appending string to textField.text only if the sum of all characters doesn't exceed max length"
Here is an example how to remove any text in UITextField; applies to iOS 10.3 and later
#IBOutlet var upcText: UITextField!
if var textRange = upcText.selectedTextRange {
if textRange.isEmpty && textRange.start != upcText.beginningOfDocument {
// text has not been selected and the cursor is not at the beginnning
// then create new range to delete previous character
textRange = upcText.textRange(from: upcText.position(from: textRange.start, offset: -1)!, to: textRange.start)!
}
upcText.replace(textRange, withText: "")
}

Text Validation - user can only select a number once

I am trying to create a questionnaire, in which the user shall rate statements from 1-7.
The user rates the statements by filling UITextFields with a value between 1-7.
I have made it so that the user can only write in values between 1-7 and that they only can write one character per textfield.
What I want to do now is to prevent the user from using that value more than once.
This is the code that I have so far.
#define CHARACTERS #"1234567"
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
// These are the characters that are ~not~ acceptable
NSCharacterSet *unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:CHARACTERS] invertedSet];
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if (([[string componentsSeparatedByCharactersInSet:unacceptedInput] count] >1 ) | ([newString length] > 1) )
return NO;
else
return YES;
}
I would use a dictionary. Whenever a number is chosen in a textfield, check if the dictionary has that value. If it doesn't have the value, the value entered is correct. Add the entry into the dictionary now and then let the user proceed to the next textfield. If you repeat this process for all the textfields you will be able to solve this problem pretty easily.
You can do one thing give tag to all textfields and check whether string you entered in current textfield is same as text of all other textfields then return NO.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
// These are the characters that are ~not~ acceptable
NSCharacterSet *unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:CHARACTERS] invertedSet];
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if (([[string componentsSeparatedByCharactersInSet:unacceptedInput] count] >1 ) | ([newString length] > 1) )
return NO;
else
//Call function to check if string is same as other textfield.text then return NO else YES
}
else
another option is you can take 1 string and save textfield.text in that string and update all time when you enter and check with it whether the string you are entering is in substring of that string.
best of luck
At the time of creating UITextField set the keyboardType as UIKeyboardTypeNumberPad.
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([[textField text]length]==1)
//perform condition
else
//perform condition
}
this might help

how to validate a Text field to accept only Characters at run Time in iphone

i want to validate a text field to accept only characters while typing in it.....
I use the following for integers but I guess you could easily modify it to scan for strings/chars (see NSScanner):
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *resultingString = [textField.text stringByReplacingCharactersInRange: range withString: string];
// This allows backspace
if ([resultingString length] == 0) {
return true;
}
NSUInteger holder;
NSScanner *scan = [NSScanner scannerWithString: resultingString];
return [scan scanInteger: &holder] && [scan isAtEnd];
}
Don't forget to set the UITextField delegate appropriately :)