简体   繁体   中英

Allow German UMLAUT characters in UITextfield ios

I am accepting a form from user, in which one of the textfieled accepts the characters, digit and only some special characters. (not all)

For this, I wrote MACRO which will allow the required input. But when I change keyboard language to German or when I enter any UMLAUT character like Ö or Ä it consider it as special character. Same case happens for CHINA language.

I am not using any localisation, my app language is English only. This scenario happens when user is from GERMANY and tries to enter his name in UITextfield.

I want to add those charcters as well. Here is my MACRO

#define ACCEPTABLE_CHARACTERS @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+{-}()-/:'?,. "

How to achive this,

My textfield should accept all languages and only +{-}()-/:'?,. these special characters.

Thanks in advance.

It's a bit more tricky with Unicode, but there is support for that. You can use NSCharacterSet for this purpose.

First create a character set object that is a combination of what you want, letters, white space, digits and your special characters.

NSMutableCharacterSet *set = [NSMutableCharacterSet letterCharacterSet];
[set formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];
[set formUnionWithCharacterSet:[NSCharacterSet decimalDigitCharacterSet]];
[set formUnionWithCharacterSet:[NSCharacterSet characterSetWithCharactersInString:@"+{-}()-/:'?,."]];

Then create the reverse of this.

NSCharacterSet *invertedSet = [set invertedSet];

Now, get the string you want to check. Try to find any characters from your inverted set. This will find any characters except the ones that you allow.

NSString *string = self.textField.text;
NSRange range = [string rangeOfCharacterFromSet:invertedSet];
if (range.location != NSNotFound) {
    NSLog(@"Illegal character found at %d!", (int)range.location);
} else {
    NSLog(@"OK");
}

If you find an unwanted character, you will have the position of the first occurrence of the unwanted character in the range.location .

Read more

https://developer.apple.com/documentation/foundation/nscharacterset?language=objc

Here is your solutions in Swift4

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let  char = string.cString(using: .utf8)!
    let isBackSpace = strcmp(char, "\\b")
    if (isBackSpace == -92) {
        return true
    }
    if textField == <YourTextField> {

        // Allowed only letters to enter in TextField.
        let characterSet = CharacterSet(charactersIn: ACCEPTABLE_CHARACTERS)
        let filterString: [String] = string.components(separatedBy: characterSet) as [String]
        let str = filterString.joined(separator: "")
        return !(string == str)
    }
    return true
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM